Java Variables:
Java is a strongly typed programming language. This means that every variable must have a data type associated with it. For example, a variable could be declared to use one of the eight primitive data types: byte, short, int, long, float, double, char or boolean.
Data Type | Default Value | Default size |
---|---|---|
boolean | false | 1 bit |
char | ‘\u0000’ | 2 byte |
byte | 0 | 1 byte |
short | 0 | 2 byte |
int | 0 | 4 byte |
long | 0L | 8 byte |
float | 0.0f | 4 byte |
double | 0.0d | 8 byte |
Why do we use static keyword in Java?
The static keyword in Java means that the variable or function is shared between all instances of that class as it belongs to the type, not the actual objects themselves. So if you have a variable: private static int i = 0; and you increment it ( i++ ) in one instance, the change will be reflected in all instances.
Relevant Code:
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 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 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 |
System.out.println("I am In Main Method"); //variable Declaration // <Data_type> <name_of_the_var> = <value>; int i_var_1,i_var_2; double d_result; String s_var = "ITPA"; boolean b_bool = true; //Array declaration int [] numbers = {10, 20, 30, 40, 50}; i_var_1 = 50; i_var_2 = 100; //Below Will give Result as 0.0 as int divided by int will always give int d_result = i_var_1/i_var_2; //Type Casting in java. This will give Correct Result d_result = (double)i_var_1/(double)i_var_2; //Convert from String to Int String s_num_1 = "14"; String s_num_2 = "7"; int i_result_1 = Integer.parseInt(s_num_1)/Integer.parseInt(s_num_2); System.out.println("Converting from String to Integer; Divison Result: " + i_result_1); //Condition Operators in Java // == equal to // != not equal to // > greater than // >= greater than or equal to // < less than // <= less than or equal to // && Conditional-AND // || Conditional-OR //If conditions int value1 = 1; int value2 = 2; if((value1 == 1) && (value2 == 2)) System.out.println("value1 is 1 AND value2 is 2"); if((value1 == 1) || (value2 == 1)) System.out.println("value1 is 1 OR value2 is 1"); //If Else Condtion int x = 30; if( x < 20 ){ System.out.print("This is if statement"); }else{ System.out.print("This is else statement"); } // Switch Case in Java int month = 8; String monthString; switch (month) { case 1: monthString = "January"; break; case 2: monthString = "February"; break; case 3: monthString = "March"; break; case 4: monthString = "April"; break; case 5: monthString = "May"; break; case 6: monthString = "June"; break; case 7: monthString = "July"; break; case 8: monthString = "August"; break; case 9: monthString = "September"; break; case 10: monthString = "October"; break; case 11: monthString = "November"; break; case 12: monthString = "December"; break; default: monthString = "Invalid month"; break; } System.out.println(monthString); |