Eventhough there is a static variable named a, the local variable takes
the precedence. So a compilation error occurs saying, 'a' may not be initialized
will not compile because "two" is not associated with any loop
see API for details.
int x=100; float y = 100.0f;
if(x==y) {System.out.println("equal");}//converts int to float
output :
equal
byte b=2;
byte b1=3;
b=b*b1;//illegal
will not compile because before multipying both b and b1 will be converted
to int and int can't be assigned to byte.
creating a character array :
String s = "Hello there";
char[] arr;
arr=s.toCharArray();
System.out.println(arr[1]);//prints ""e"
creating String from character array :
char[] c={'H','e','l','l','o'};
String s = String.copyValueOf(c);
class test {
static int i[];
public static void main(String arg[]) {
System.out.println(i[2]); //gives NullPointerException
}
}
int k[][]=new int[10][20];
System.out.println(k.length); //prints 10
int k[][]={{1,2,3,4},{1,2,3},{5,4,6,4}};
System.out.println(k.length); //prints 3
int num[] = new int[]{1,2,3};
1 Class Number {
2 int i;
3 }
4 public class Assignment {
5 public static void main(String[] args) {
6 Number n1 = new Number();
7 Number n2 = new Number();
8 n1.i = 9;
9 n2.i = 47;
10 System.out.println("1: n1.i: " + n1.i +
", n2.i: " + n2.i);
11 n1 = n2;
12 System.out.println("2: n1.i: " + n1.i +
", n2.i: " + n2.i);
13 n1.i = 27;
14 System.out.println("3: n1.i: " + n1.i +
", n2.i: " + n2.i);
15 }
16 }
Output:
1: n1.i: 9, n2.i: 47
2: n1.i: 47, n2.i: 47
3: n1.i: 27, n2.i: 27
Changing the n1 object, changes the n2 object. This is because both n1 and n2 contain the same reference, which is pointing to the same object.
Instead, if u change line number 11 to n1.i = n2.i;
The output will be
1: n1.i: 9, n2.i: 47
2: n1.i: 47, n2.i: 47
3: n1.i: 27, n2.i: 47