Hello World


1

2

3

4

5

class HelloWorld {

public static void main( String[ ] args ) {

System.out.println("Hello World");

}

}



Output

Hello World

class HelloWorld has a function named main.
function main has a function-call to another function named println.
function println prints the text "Hello World" to the screen.


class

indicates that a new class is defined. A class is a blueprint for creating objects (instances of a class). Each object created from a class can store the same kind of data and has the same functionality. (classes)



HelloWorld

is the name of the class being defined. (naming rules) (naming conventions)



{ }

code for the class goes inside the curly braces.



public

is an accessmodifier and means that all functions and classes have access to the function. public can also be used for datamembers and classes. By default (when no accessmodifier is being used), datamembers, functions and classes are assigned an accessmodifier called package-private which means they are accessible within the same package. (access modifier) (datamembers) (package)



static

is a concept beyond this discussion. (static)



void

is the returntype for a function. void means that the function is not returning anything.



main

is the name of the function. (naming rules) (naming conventions)



( )

parameters go inside the parenteces. Parameters are used to pass information to function. Each parameter has a datatype that can store e.x. a number or text. (Parameters)



String

is a datatype that can store a text. (String)



[ ]

indicates that the parameter is not just of type String but rather an array (list) of Strings. This means that a list containing 0, 1, or more Strings will be passed to the function main when the function is called. This is done by the system since the system calls the function main. (Arrays)



args

is the name of the parameter being passed. The parameter can be named whatever you like, but is commonly named "args" which is short for arguments. This parameter will not be used here, but has to be there since the system will call a method named "main" that takes one parameter of type String[ ] (String array).



Datatypes


Primitive Types

primitive types are predefined by the Java language. A variable or a constant is used to store one value of a primitive type. The value of a variable can be changed over time. However, a constant is assigned a value when it is declared and cannot be changed over time.


Declare a variable

datatype variableName;


Initialize a variable

variableName = value;


Declare and initialize a variable in one statement

datatype variableName = value;


Declare and initialize a constant

final datatype variableName = value;
(in Java, the keyword final is used to indicate that a constant is created)


int

is, by default, a 32-bit signed two's complement integer in the range -2147483648 (-231) to 2147483647 (231-1). If a variable of type int is assigned the value 2,147,483,647 and then incremented by 1, the new value would be -2147483648. Similarly if a variable of type int is assigned the value -2147483648 and then decremented by 1, the new value would be 2147483647. This behavior is due to the way signed two's complement integers are represented in memory (more info). In Java SE 8 and later, int can be used to represent an unsigned 32-bit integer in the range 0 to 4294967296 (232). If a variable of type unsigned int is assigned the value 4294967296 and then incremented by 1, the new value would be 0. Similarly if a variable of type unsigned int is assigned the value 0 and then debremented by 1, the new value would be 4294967296. This behavior is due to the way unsigned integers are represented in memory (more info).



double

is a double-precision 64-bit IEEE 754 floating point. The type double is used to store decimal values.



char

is a single 16-bit Unicode character with a minimum value of '\u0000' (hexadecimal) or 0 (decimal) and a maximum value of '\uffff' (hexadecimal) or 65535 (decimal). A char can store characters such as alphabetic letters and numbers, but also non-printable characters (for a complete list). All assignment statements below assigns the character 'A' to a variable of type char:


char character = 'A'

char character = '\u00'

char character = 65;

// assigning a char value

// assigning a hexadecimal value

// assigning an int value



boolean

has two possible values, true and false. boolean values are used in conditional statements.



Default Values


int

0


double

0.0


char

'\u0000'


boolean

false




1

2

3

4

5

6

7

8

9

10

11

class Datatypes {

public static void main( String[ ] args ) {

// Variables of different types are declared

int integer;

double decimal;

char character;

boolean condition;


// The default values are printed;

System.out.println("integer: " + integer);

System.out.println("decimal: " + decimal);



12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

System.out.println("character: " + character);

System.out.println("condition: " + condition);


// The variables are assigned new values.

integer = 3;

decimal = 5.7;

character = 'g'

condition = true;


// The new values are printed.

System.out.println('integer: ' + integer);

System.out.println('decimal: ' + decimal);

System.out.println('character: ' + character);

System.out.println('condition: ' + condition);

}

}



System.out.println( );

System is a class defined by the Java language and includes among other things an object named out of type PrintStream (a class defined by the Java language). The class PrintStream has a function named println that takes one parameter of type String. In Java, text between double quotation marks ("") represents a String value.



String + other type

The addition operator (+) can be used to concatenate text. If one of the operands is not of type String, it will be automatically converted to a String and then concatenated with the other operand as one String. This is neccessary since the function println only takes one parameter of type String. (addition operator)


Operators


Assignment

=

assignment operator. Assigns the value on its right to the variable on its left.



Arithmetic

+

addition operator. Also used for String concatenation.


-

subtraction operator.


*

multiplication operator.



Unary

+

unary plus operator.


-

unary minus operator. Negates an expression.


++

increment operator. Increments a variable by 1.


--

decrement operator. Decrements a variable by 1.


!

logical complement operator. Inverts a boolean value.


Selection Sort


1

2

3

4

5

6

7

8

9

10

11

12

public static void selectionSort(int[ ] array) {

for(int i = 0; i < array.length; i++) {

int minIndex = i;

for(int j = i+1; j < array.length; j++) {

if(array[j] < array[minIndex])

minIndex = j;

}

int temp = array[p];

array[p] = array[j];

array[j] = temp;

}

}



Insertion Sort


1

2

3

4

5

6

7

8

9

10

11

12

13

public static void insertionSort(int[ ] array) {

for(int i = 1; i < array.length; i++) {

for(int j = i; j > 0; j--) {

if(array[j] < array[j-1]) {

int temp = array[j-1];

array[j-1] = array[j];

array[j] = temp;

}

else

break;

}

}

}



Linked List


Singly Linked List


1

2

3

4

5

6

7

8

class Node {

V value; // The value associated with a Node

Node next; // Pointer to the next Node in the list


public Node(V value) {

this.value = value;

}

}



1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

class SinglyLinkedList {

private Node front;


public boolean isEmpty( ) {

return (front != null);

}


public int size ( ) {

Node curr = front; // curr is short for current

int count = 0; // a counter that counts every Node in the list


// if curr is not refering to null it is refering to a Node

while( curr != null ) {

// every time curr referes to a Node, count is

// incremented

count++;

// the address of the next Node is assigned to curr

curr = curr.next;

}

return count;

}




23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

public void insertFront ( V value ) {

Node newNode = new Node(value); // create a Node


// if the list is not empty, set the member next of newNode to

// front

if( !isEmpty( ) )

newNode.next = front;

front = newNode;

}


public void insertBack ( V value ) {

Node newNode = new Node(value); // create a Node


if ( !isEmpty( ) )

back.next = newNode;

else

front = newNode

back = newNode;

}


public void insertAt ( int index, V value ) {

Node newNode = new Node(value);

Node current = front;

int count = 1; // one more than current position


// special case: if inserting at first location, there is no node

// before

if ( index == 1 ) {

newNode.next = front;

front = newNode;

}




55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

while ( current != null ) {

if ( index == count ) {

newNode.next = current.next;

current.next = newNode;

return;

}

current = current.next;

count++;

}

// index not found

}


public void removeFront ( ) {


// if the list is not empty, set front to the member next of front

if ( !( isEmpty( ) ) )

front = front.next;

}


public void removeBack ( ) {

if ( isEmpty( ) )

return;

else if ( front.next == null ) {

front = null;

return;

}

Node current = front;


while ( current.next.next != null ) {

current = current.next;

}


current.next = null;

}


public void removeAt ( int index ) {

Node current = front;

int count = 1; // one more than current position




94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

129

while ( current != null ) {

if ( index == count ) {

current.next = current.next.next;

return;

}

current = current.next;

count++;

}

// index not found

}


public V set ( int index, V value ) {

Node newNode = new Node( );

Node current = front;

int count = 0;

V oldValue;

while ( current != null ) {

if ( index == count ) {

oldValue = current.value;

current.value = value;

return oldValue;

}

current = current.next;

}

// index not found

return null;

}


public void print ( ) {

Node current = front;

while ( current != null ) {

System.out.println("Value of Node current " + current);

current = current.next;

}

}

}



Stack


1

2

3

4

5

6

7

8

class Node {

V value; // The value associated with a Node

Node next; // Pointer to the next Node in the list


public Node(V value) {

this.value = value;

}

}



1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

class Stack {

private Node top;


public boolean isEmpty ( ) {

return top != null;

}


public void push ( V value ) {

Node newNode = new Node ( value );

newNode.next = top;

top = newNode;

}


public V pop( ) {

V value;

if ( !isEmpty( ) ) {

value = top.value;

top = top.next;

return value;

}

// error handling for empty list

}


public V peek ( ) {

if ( !isEmpty( ) )

return top.value;

// error handling for empty list

}

}



© o2programming.com