Basic JAVA Programming Notes 1


This post describes basic JAVA Programming Notes.

  • Comments:

Java Comments can be specified in 3 styles:

    1. Mark each line with a // at the beginning of each comment line.
    2. /* and */ comment delimiters can be used tp block off a longer comment.
    3. Third kind of comment can be used to generate documentation automatically. This comment uses a /** to start and a */ to end.
  • Data Types:

    1. In Java every variable must have a declared type.
    2. 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:

    1. 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:

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.

or

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.

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:

    1. 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.
    2. Increment and decrement operators: n++ adds 1 to the current value of the variable n , and n- – subtracts 1 from it.
    3. Relational operators ( equality : == , inequality : != , less than : < , greater than : > , less than or equal : <= , and greater than or equal : >= ).
    4. Java uses && for the logical “and” operator and || for the logical “or” operator.
    5. 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.

  • Enumerated Types:

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.

  • 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:

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.

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.

5.  length() method on string object returns the length of the string.

For example grtng.length() returns 5 .


Profile photo of Siva

About Siva

Senior Hadoop developer with 4 years of experience in designing and architecture solutions for the Big Data domain and has been involved with several complex engagements. Technical strengths include Hadoop, YARN, Mapreduce, Hive, Sqoop, Flume, Pig, HBase, Phoenix, Oozie, Falcon, Kafka, Storm, Spark, MySQL and Java.


Leave a comment

Your email address will not be published. Required fields are marked *

One thought on “Basic JAVA Programming Notes