This post describes basic JAVA Programming Notes.
- Comments:
Java Comments can be specified in 3 styles:
- Mark each line with a // at the beginning of each comment line.
- /* and */ comment delimiters can be used tp block off a longer comment.
- Third kind of comment can be used to generate documentation automatically. This comment uses a /** to start and a */ to end.
-
Data Types:
- In Java every variable must have a declared type.
- Eight primitive types are there in Java. (4 integer types; 2 floating-point number types; 1 character type char and 1 boolean type).
Data Type | Size |
int | 4 bytes |
short | 2 bytes |
long | 8 bytes |
byte | 1 byte |
float | 4 bytes |
double | 8 bytes |
char | 2 bytes (16 bit UTF-16 encoding schema) |
boolean | 1 byte |
boolean type has two values, false and true.
-
Variables:
- Every variable declaration must have a type and variable name must begin with a letter (‘A’–’Z’ , ‘a’–’z’ , ‘_’ , or any Unicode character).
Sample declaration:
1 2 3 |
double salary; int days; |
Multiple declarations on a single line:
1 2 |
int i, j; // both are integers |
2. We must explicitly initialize variables by means of an assignment statement. We can never use the values of uninitialized variables.
1 2 3 |
int i, j; i = 10 ; j = 20 ; |
or
1 2 |
int i = 10 , j = 20 ; |
3. We can put variable declarations anywhere in our code.
-
Constants:
Constants are defined with final keyword. And class constants are declared with static final.
1 2 3 |
final double PI = 3.14 ; // constants static final double CM_PER_INCH = 2.54; |
Class constant is available to multiple methods inside a class. Definition of the class constant appears outside the main() method, so that other methods in the class can also access the constant. If it is is declared as public , methods of other classes can also use the constant, as ClassName.ConstantName.
-
Operators:
- Arithmetic operators + – * / are used in Java for addition, subtraction, multiplication, and division. Integer remainder is denoted by %. For example, 15 / 2 is 7, 15 % 2 is 1, and 15.0 / 2 is 7.5.
- Increment and decrement operators: n++ adds 1 to the current value of the variable n , and n- – subtracts 1 from it.
- Relational operators ( equality : == , inequality : != , less than : < , greater than : > , less than or equal : <= , and greater than or equal : >= ).
- Java uses && for the logical “and” operator and || for the logical “or” operator.
- Java supports the ternary ?: operator which is also called conditional operator.
condition ? expression 1 : expression 2
evaluates to the first expression if the condition is true , to the second expression otherwise.
-
Conversions between Numeric Types
When two values with a binary operator (such as n + f where n is an integer and f is a floating-point value) are combined, both operands are converted to a common type before the operation is carried out.
-
If either of the operands is of type double , the other one will be converted to a double .
-
Otherwise, if either of the operands is of type float , the other one will be converted to a float .
-
Otherwise, if either of the operands is of type long , the other one will be converted to a long .
-
Otherwise, both operands will be converted to an int .
A variable can be converted to other data type by using casts. The syntax for casting is to give the target type in parentheses, followed by the variable name.
1 2 3 |
double d = 10.5555; int i = (int) d; |
If you try to cast a number of one type to another that is out of the range for the target type, the result will be a truncated number that has a different value. For example, (byte) 300 is actually 44.
We cannot cast between Boolean values and any numeric type.
-
Enumerated Types:
An enumerated type has a finite number of named values. For example:
1 2 |
enum Size { SMALL, MEDIUM, LARGE, EXTRA_LARGE }; |
Now you can declare variables of this type:
1 2 |
Size s = Size.MEDIUM; |
A variable of type Size can hold only one of the values listed in the type declaration or the special value null that indicates that the variable is not set to any value at all.
-
Strings:
1. Java does not have a built-in string type. Instead, the standard Java library contains a predefined class called, String. Each quoted string is an instance of the String class:
1 2 3 4 5 |
String e = ""; // an empty string String greeting = "Hello" String greeting = "Hello"; String s = greeting.substring(0, 3); |
The second parameter of substring is the first position that you do not want to copy. In our case, we want to copy positions 0, 1, and 2 (from position 0 to position 2 inclusive).
2. We can use + sign to concatenate two strings. When we concatenate a string with a value that is not a string, the latter is converted to a string. For example,
1 2 3 |
int age = 13; String rating = "PG" + age; |
sets rating to the string “PG13” .
3. Strings are Immutable. String characters cannot be changed. To modify the string in Java Concatenate the substring that you want to keep with the characters that you want to replace.
1 2 3 |
String greeting = “Hello” ; greeting = greeting.substring(0, 3) + "p!"; |
This declaration changes the current value of the greeting variable to “Help!” . Because we cannot change the individual characters in a Java string, the documentation refers to the objects of the String class as being immutable.
however, we can change the contents of the string variable greeting and make it refer to a different string, just as we can make a numeric variable currently holding the value 3 hold the value 4.
4. To test whether two strings are equal, use the equals method. The expression
s.equals(t)
returns true if the strings s and t are equal, false otherwise.
Do not use the == operator to test whether two strings are equal! It only determines
whether or not the strings are stored in the same location. Sure, if strings are in the same location, they must be equal. But it is entirely possible to store multiple copies of identical strings in different places.
1 2 3 4 5 |
String grtng = "Hello"; //initialize grtng to a string if (grtng == "Hello") . . . // probably true if (grtng.equals(“Hello”)) // defenitely true if (grtng.substring(0, 3) == "Hel") . . . // probably false |
5. length() method on string object returns the length of the string.
For example grtng.length() returns 5 .
Pingback: Basic JAVA Programming Notes - Hadoop Online Tu...