This post describes basic JAVA Programming Notes.
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.
- 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).
|char||2 bytes (16 bit UTF-16 encoding schema)|
boolean type has two values, false and true.
- Every variable declaration must have a type and variable name must begin with a letter (‘A’–’Z’ , ‘a’–’z’ , ‘_’ , or any Unicode character).
Multiple declarations on a single line:
2. We must explicitly initialize variables by means of an assignment statement. We can never use the values of uninitialized variables.
3. We can put variable declarations anywhere in our code.
Constants are defined with final keyword. And class constants are declared with static final.
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.
- 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.
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.
An enumerated type has a finite number of named values. For example:
Now you can declare variables of this type:
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.
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:
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,
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.