Anda di halaman 1dari 20

1.

class Basics1 {
2. public static void main(String[] args) {}
3. }
4. class Basics2 {
5. protected static void main(String[] args) {}
6. }
7. class Basics3 {
8. private static void main(String[] args) {}
9. }
What is the result of attempting to compile each of the three class declarations

Section 12.1.4 of the Java Language Specification states the


following, "The method main must be declared public, static,
and void. It must accept a single argument that is an array of
Strings." The rule is not enforced by the compiler, but should
instead be enforced by the Java Virtual Machine. However,
not every JVM enforces the rule. Even so, for the purposed
Run time error at
of the SCJP exam, the main method should be declared as
line 5. Run time
stated in the Java Language Specification. Note: the Java
error at line 8.
Language Specification provides the option to disallow the
declaration of more than one class in a single source code file
if more than one of the classes in the file is referred to by
code that exists outside of the file. The restriction is stated as
an option and not a requirement. This option is not enforced
by any compiler that I have tested.
How to determine the upper bound of a single dimensional array ?
Following example helps to determine the upper bound of a two dimensional
array with the use of arrayname.length.
public class Main {
public static void main(String args[]) {
String[] data = new String[2];
System.out.println("Size of the array is : " + data.length);

}
}
Result:
The above code sample will produce the following result.
Size of the array is: 2

How to determine the upper bound of a two dimensional array ?


Following example helps to determine the upper bound of a two dimensional
array with the use of arrayname.length.
public class Main {
public static void main(String args[]) {
String[][] data = new String[2][5];
System.out.println("Dimension 1: " + data.length);
System.out.println("Dimension 2: " + data[0].length);
}
}
Result:
The above code sample will produce the following result.
Dimension 1: 2
Dimension 2: 5

class B {
public static void main(String[] args) {
int[] a1[] = {{1,2},{3,4,5},{6,7,8,9}};
int []a2[] = {{1,2},{3,4,5},{6,7,8,9}};
int a3[][] = {{1,2},{3,4,5},{6,7,8,9}};
System.out.print(a1[0][1]+","+a2[1][2]+","+a3[2][3]);
}
}

What is the result of attempting to compile and run the above program?

Prints: 2,5,9
Arrays a1, a2 and a3 are all arrays where each element is an array of primitives
of type int. The size of each of the 3 primitive int arrays are different. The size
of the first is 2, the second is 3, and the third is 4. The declarations of a1, a2,
and a3 are different in terms of the position of the square brackets, but the result
of each is the same.
How to write an array of strings to the output console ?
Following example demonstrates writing elements of an array to the output
console through looping.
public class Welcome {
public static void main(String[] args){
String[] greeting = new String[3];
greeting[0] = "This is the greeting";
greeting[1] = "for all the readers from";
greeting[2] = "Java Source .";
for (int i = 0; i < greeting.length; i++){
System.out.println(greeting[i]);
}
}
}
Result:
The above code sample will produce the following result.
This is the greeting
For all the readers From
Java source .

Java.util.Arrays Class
Introduction
The java.util.Arrays class contains a static factory that allows arrays to be
viewed as lists.Following are the important points about Arrays:

This class contains various methods for manipulating arrays (such as


sorting and searching).

The methods in this class throw a NullPointerException if the specified


array reference is null.
Class declaration
Following is the declaration for java.util.Arrays class:
public class Arrays
extends Object
Class methods
S.N
Method & Description
.
1

static <T> List<T> asList(T... a)


This method returns a fixed-size list backed by the
specified array.

static int binarySearch(byte[] a, byte key)


This method searches the specified array of bytes for
the specified value using the binary search
algorithm.

static int binarySearch(byte[] a, int fromIndex, int


toIndex, byte key)
This method searches a range of the specified array
of bytes for the specified value using the binary
search algorithm.

static int binarySearch(char[] a, char key)


This method searches the specified array of chars for
the specified value using the binary search
algorithm.

static int binarySearch(char[] a, int fromIndex, int


toIndex, char key)
This method searches a range of the specified array

of chars for the specified value using the binary


search algorithm.
6

static int binarySearch(double[] a, double key)


This method searches the specified array of doubles
for the specified value using the binary search
algorithm.

static int binarySearch(double[] a, int fromIndex, int


toIndex, double key)
This method searches a range of the specified array
of doubles for the specified value using the binary
search algorithm.

static int binarySearch(float[] a, float key)


This method searches the specified array of floats for
the specified value using the binary search
algorithm.

static int binarySearch(float[] a, int fromIndex, int


toIndex, float key)
This method searches a range of the specified array
of floats for the specified value using the binary
search algorithm.

10

static int binarySearch(int[] a, int key)


This method searches the specified array of ints for
the specified value using the binary search
algorithm.

11

static int binarySearch(int[] a, int fromIndex, int


toIndex, int key)
This method searches a range of the specified array
of ints for the specified value using the binary search
algorithm.

12

static int binarySearch(long[] a, int fromIndex, int


toIndex, long key)
This method searches a range of the specified array
of longs for the specified value using the binary
search algorithm.

13

static int binarySearch(long[] a, long key)


This method searches the specified array of longs for
the specified value using the binary search
algorithm.

14

static int binarySearch(Object[] a, int fromIndex, int

toIndex, Object key)


This method searches a range of the specified array
for the specified object using the binary search
algorithm.
15

static int binarySearch(Object[] a, Object key)


This method searches the specified array for the
specified object using the binary search algorithm.

16

static int binarySearch(short[] a, int fromIndex, int


toIndex, short key)
This method searches a range of the specified array
of shorts for the specified value using the binary
search algorithm.

17

static int binarySearch(short[] a, short key)


This method searches the specified array of shorts
for the specified value using the binary search
algorithm.

18

static <T> int binarySearch(T[] a, int fromIndex, int


toIndex, T key, Comparator<? super T> c)
This method searches a range of the specified array
for the specified object using the binary search
algorithm.

19

static <T> int binarySearch(T[] a, T key,


Comparator<? super T> c)
This method searches the specified array for the
specified object using the binary search algorithm.

20

static boolean[] copyOf(boolean[] original, int


newLength)
This method copies the specified array, truncating or
padding with false (if necessary) so the copy has the
specified length.

21

static byte[] copyOf(byte[] original, int newLength)


This method copies the specified array, truncating or
padding with zeros (if necessary) so the copy has the
specified length.

22

static char[] copyOf(char[] original, int newLength)


This method copies the specified array, truncating or
padding with null characters (if necessary) so the
copy has the specified length.

23

static double[] copyOf(double[] original, int

newLength)
This method copies the specified array, truncating or
padding with zeros (if necessary) so the copy has the
specified length.
24

static float[] copyOf(float[] original, int newLength)


This method copies the specified array, truncating or
padding with zeros (if necessary) so the copy has the
specified length.

25

static int[] copyOf(int[] original, int newLength)


This method copies the specified array, truncating or
padding with zeros (if necessary) so the copy has the
specified length.

26

static long[] copyOf(long[] original, int newLength)


This method copies the specified array, truncating or
padding with zeros (if necessary) so the copy has the
specified length.

27

static short[] copyOf(short[] original, int


newLength)
This method copies the specified array, truncating or
padding with zeros (if necessary) so the copy has the
specified length.

28

static <T> T[] copyOf(T[] original, int newLength)


This method copies the specified array, truncating or
padding with nulls (if necessary) so the copy has the
specified length.

29

static <T,U> T[] copyOf(U[] original, int


newLength, Class<? extends T[]> newType)
This method copies the specified array, truncating or
padding with nulls (if necessary) so the copy has the
specified length.

30

static boolean[] copyOfRange(boolean[] original, int


from, int to)
This method copies the specified range of the
specified array into a new array.

31

static byte[] copyOfRange(byte[] original, int from,


int to)
This method copies the specified range of the
specified array into a new array.

32

static char[] copyOfRange(char[] original, int from,

int to)
This method copies the specified range of the
specified array into a new array.
33

static double[] copyOfRange(double[] original, int


from, int to)
This method copies the specified range of the
specified array into a new array.

34

static float[] copyOfRange(float[] original, int from,


int to)
This method copies the specified range of the
specified array into a new array.

35

static int[] copyOfRange(int[] original, int from, int


to)
This method copies the specified range of the
specified array into a new array.

36

static long[] copyOfRange(long[] original, int from,


int to)
This method copies the specified range of the
specified array into a new array.

37

static short[] copyOfRange(short[] original, int from,


int to)
This method copies the specified range of the
specified array into a new array.

38

static <T> T[] copyOfRange(T[] original, int from,


int to)
This method copies the specified range of the
specified array into a new array.

39

static <T,U> T[] copyOfRange(U[] original, int


from, int to, Class<? extends T[]> newType)
This method copies the specified range of the
specified array into a new array.

40

static boolean deepEquals(Object[] a1, Object[] a2)


This method returns true if the two specified arrays
are deeply equal to one another.

41

static int deepHashCode(Object[] a)


This method returns a hash code based on the "deep
contents" of the specified array.

42

static String deepToString(Object[] a)

This method returns a string representation of the


"deep contents" of the specified array.
43

static boolean equals(boolean[] a, boolean[] a2)


This method returns true if the two specified arrays
of booleans are equal to one another.

44

static boolean equals(byte[] a, byte[] a2)


This method returns true if the two specified arrays
of bytes are equal to one another.

45

static boolean equals(char[] a, char[] a2)


This method returns true if the two specified arrays
of chars are equal to one another.

46

static boolean equals(double[] a, double[] a2)


This method returns true if the two specified arrays
of doubles are equal to one another.

47

static boolean equals(float[] a, float[] a2)


This method returns true if the two specified arrays
of floats are equal to one another.

48

static boolean equals(int[] a, int[] a2)


This method returns true if the two specified arrays
of ints are equal to one another.

49

static boolean equals(long[] a, long[] a2)


This method returns true if the two specified arrays
of longs are equal to one another.

50

static boolean equals(Object[] a, Object[] a2)


This method returns true if the two specified arrays
of Objects are equal to one another.

51

static boolean equals(short[] a, short[] a2)


This method returns true if the two specified arrays
of shorts are equal to one another.

52

static void fill(boolean[] a, boolean val)


This method assigns the specified boolean value to
each element of the specified array of booleans.

53

static void fill(boolean[] a, int fromIndex, int


toIndex, boolean val)
This method assigns the specified boolean value to
each element of the specified range of the specified
array of booleans.

54

static void fill(byte[] a, byte val)

This method assigns the specified byte value to each


element of the specified array of bytes.

55

static void fill(byte[] a, int fromIndex, int toIndex,


byte val)
This method assigns the specified byte value to each
element of the specified range of the specified array
of bytes.

56

static void fill(char[] a, char val)


This method assigns the specified char value to each
element of the specified array of chars.

57

static void fill(char[] a, int fromIndex, int toIndex,


char val)
This method assigns the specified char value to each
element of the specified range of the specified array
of chars.

58

static void fill(double[] a, double val)


This method assigns the specified double value to
each element of the specified array of doubles.

59

static void fill(double[] a, int fromIndex, int toIndex,


double val)
This method assigns the specified double value to
each element of the specified range of the specified
array of doubles.

60

static void fill(float[] a, float val)


This method assigns the specified float value to each
element of the specified array of floats.

61

static void fill(float[] a, int fromIndex, int toIndex,


float val)
This method assigns the specified float value to each
element of the specified range of the specified array
of floats.

62

static void fill(int[] a, int val)


This method assigns the specified int value to each
element of the specified array of ints.

63

static void fill(int[] a, int fromIndex, int toIndex, int


val)
This method assigns the specified int value to each
element of the specified range of the specified array
of ints.

64

static void fill(long[] a, int fromIndex, int toIndex,


long val)
This method assigns the specified long value to each
element of the specified range of the specified array
of longs.

65

static void fill(long[] a, long val)


This method assigns the specified long value to each
element of the specified array of longs.

66

static void fill(Object[] a, int fromIndex, int toIndex,


Object val)
This method assigns the specified Object reference
to each element of the specified range of the
specified array of Objects.

67

static void fill(Object[] a, Object val)


This method assigns the specified Object reference
to each element of the specified array of Objects.

68

static void fill(short[] a, int fromIndex, int toIndex,


short val)
This method assigns the specified short value to each
element of the specified range of the specified array
of shorts.

69

static void fill(short[] a, short val)


This method assigns the specified short value to each
element of the specified array of shorts.

70

static int hashCode(boolean[] a)


This method returns a hash code based on the
contents of the specified array.

71

static int hashCode(byte[] a)


This method returns a hash code based on the
contents of the specified array.

72

static int hashCode(char[] a)


This method returns a hash code based on the
contents of the specified array.

73

static int hashCode(double[] a)


This method returns a hash code based on the
contents of the specified array.

74

static int hashCode(float[] a)


This method returns a hash code based on the

contents of the specified array.


75

static int hashCode(int[] a)


This method returns a hash code based on the
contents of the specified array.

76

static int hashCode(long[] a)


This method returns a hash code based on the
contents of the specified array.

77

static int hashCode(Object[] a)


This method returns a hash code based on the
contents of the specified array.

78

static int hashCode(short[] a)


This method returns a hash code based on the
contents of the specified array.

79

static void sort(byte[] a)


This method sorts the specified array of bytes into
ascending numerical order.

80

static void sort(byte[] a, int fromIndex, int toIndex)


This method sorts the specified range of the
specified array of bytes into ascending numerical
order.

81

static void sort(char[] a)


This method sorts the specified array of chars into
ascending numerical order.

82

static void sort(char[] a, int fromIndex, int toIndex)


This method sorts the specified range of the
specified array of chars into ascending numerical
order.

83

static void sort(double[] a)


This method sorts the specified array of doubles into
ascending numerical order.

84

static void sort(double[] a, int fromIndex, int


toIndex)
This method sorts the specified range of the
specified array of doubles into ascending numerical
order.

85

static void sort(float[] a)


This method sorts the specified array of floats into
ascending numerical order.

86

static void sort(float[] a, int fromIndex, int toIndex)


This method sorts the specified range of the
specified array of floats into ascending numerical
order.

87

static void sort(int[] a)


This method sorts the specified array of ints into
ascending numerical order.

88

static void sort(int[] a, int fromIndex, int toIndex)


This method sorts the specified range of the
specified array of ints into ascending numerical
order.

89

static void sort(long[] a)


This method sorts the specified array of longs into
ascending numerical order.

90

static void sort(long[] a, int fromIndex, int toIndex)


This method sorts the specified range of the
specified array of longs into ascending numerical
order.

91

static void sort(Object[] a)


This method sorts the specified array of objects into
ascending order, according to the natural ordering of
its elements.

92

static void sort(Object[] a, int fromIndex, int


toIndex)
This method sorts the specified range of the
specified array of objects into ascending order,
according to the natural ordering of its elements.

93

static void sort(short[] a)


This method sorts the specified array of shorts into
ascending numerical order.

94

static void sort(short[] a, int fromIndex, int toIndex)


This method sorts the specified range of the
specified array of shorts into ascending numerical
order.

95

static <T> void sort(T[] a, Comparator<? super T>


c)
This method sorts the specified array of objects
according to the order induced by the specified
comparator.

96

static <T> void sort(T[] a, int fromIndex, int toIndex,


Comparator<? super T> c)
This method sorts the specified range of the
specified array of objects according to the order
induced by the specified comparator.

97

static String toString(boolean[] a)


This method returns a string representation of the
contents of the specified array of boolean.

98

static String toString(byte[] a)


This method returns a string representation of the
contents of the specified array of bytes.

99

static String toString(char[] a)


This method returns a string representation of the
contents of the specified array of chars.

static String toString(double[] a)


100 This method returns a string representation of the
contents of the specified array of doubles.
static String toString(float[] a)
101 This method returns a string representation of the
contents of the specified array of floats.
static String toString(int[] a)
102 This method returns a string representation of the
contents of the specified array of ints.
static String toString(long[] a)
103 This method returns a string representation of the
contents of the specified array of longs.
static String toString(Object[] a)
104 This method returns a string representation of the
contents of the specified array of ints.
static String toString(short[] a)
105 This method returns a string representation of the
contents of the specified array of shorts.

Methods inherited
This class inherits methods from the following classes:

java.util.Object

How to extend an array after initialisation?


Following example shows how to extend an array after initialization by
creating an new array.
public class Main {
public static void main(String[] args) {
String[] names = new String[] { "A", "B", "C" };
String[] extended = new String[5];
extended[3] = "D";
extended[4] = "E";
System.arraycopy(names, 0, extended, 0, names.length);
for (String str : extended){
System.out.println(str);
}
}
}
Result:
The above code sample will produce the following result.
A
B
C
D
E

How to remove an element of array?


Following example shows how to remove an element from array.
import java.util.ArrayList;
public class Main {

public static void main(String[] args) {


ArrayList objArray = new ArrayList();
objArray.clear();
objArray.add(0,"0th element");
objArray.add(1,"1st element");
objArray.add(2,"2nd element");
System.out.println("Array before removing an element"+objArray);
objArray.remove(1);
objArray.remove("0th element");
System.out.println("Array after removing an element"+objArray);
}
}
Result:
The above code sample will produce the following result.
Array before removing an element[0th element, 1st element, 2nd element]
Array after removing an element[2nd element]
A sample example to present the inheritance in Java is shown below:
Animal.java:
01 public class Animal {
02 public Animal() {
03
System.out.println("A new animal has been created!");
04 }
05
06 public void sleep() {
07
System.out.println("An animal sleeps...");
08 }
09
10 public void eat() {
11
System.out.println("An animal eats...");
12 }
13 }
Bird.java:
01 public class Bird extends Animal {
02 public Bird() {

03
super();
04
System.out.println("A new bird has been created!");
05 }
06
07 @Override
08 public void sleep() {
09
System.out.println("A bird sleeps...");
10 }
11
12 @Override
13 public void eat() {
14
System.out.println("A bird eats...");
15 }
16 }
Dog.java:
01 public class Dog extends Animal {
02 public Dog() {
03
super();
04
System.out.println("A new dog has been created!");
05 }
06
07 @Override
08 public void sleep() {
09
System.out.println("A dog sleeps...");
10 }
11
12 @Override
13 public void eat() {
14
System.out.println("A dog eats...");
15 }
16 }
MainClass.java:
01 public class MainClass {
02 public static void main(String[] args) {
03
Animal animal = new Animal();
04
Bird bird = new Bird();
05
Dog dog = new Dog();
06
07
System.out.println();

08
09
animal.sleep();
10
animal.eat();
11
12
bird.sleep();
13
bird.eat();
14
15
dog.sleep();
16
dog.eat();
17 }
18 }
In this example we created three distinct classes, Animal, Dog and Bird.
Both Dog and Bird classes extend the Animalclass and thus, they inherit its
members and methods. Moreover, as we can see below, each class overrides the
methods of Animal and thus, both the Dog and Bird classes redefine the
functionality of Animals methods.
A sample execution is shown below:
A new animal has been created!
A new animal has been created!
A new bird has been created!
A new animal has been created!
A new dog has been created!

An animal sleeps...
An animal eats...
A bird sleeps...
A bird eats...
A dog sleeps...
A dog eats...

Default Value of Data Types in Java :

Data Type

Default Value (for fields)

byte

short

int

long

0L

float

0.0f

double

0.0d

char

u0000

String (or any object)

null

boolean

false

Live Example : Default value of Data Type


Sample Program that will illustrate Default Value Stored in Each Primitive Data
Type Variable
public class DefaultValue {
static boolean bool;
static byte by;
static char ch;
static double d;
static float f;
static int i;
static long l;
static short sh;
static String str;
public static void main(String[] args) {
System.out.println("Bool :" + bool);
System.out.println("Byte :" + by);
System.out.println("Character:" + ch);
System.out.println("Double :" + d);
System.out.println("Float :" + f);
System.out.println("Integer :" + i);

System.out.println("Long :" + l);


System.out.println("Short :" + sh);
System.out.println("String :" + str);
}
}
Output :
[46860]
Bool :false
Byte :0
Character:
Double :0.0
Float :0.0
Integer :0
Long :0
Short :0
String :null