Labels

Showing posts with label Core JAVA. Show all posts
Showing posts with label Core JAVA. Show all posts

Thursday, 2 July 2015

Blocks in JAVA

BLOCK IN JAVA

JAVA supports 2 types of blocks.
1. Static block
2. Non static block

If the block is declared within the static keyword then the block is known as static block.
The static block always executes before the main method, which is executed by the JVM.

If the block is declared without any static keyword then it is a Non Static block.
Non static block always executes before the constructor called by the user.
Static block is executed only for a single time for the whole life cycle of the program. Whereas non static block may execute "n" number of times, as it depends on the user
The existence of the main method will be checked by the JVM before the execution of the Static block.

Static block execution depends on the JVM. Non static block execution depends on the User.

You can write more than 1 static or non static blocks within a single class, as it is accessed top to bottom manner.

VARIABLES IN JAVA

VARIABLES IN JAVA

JAVA supports 6 types of variables
1. Local variable
2. Static variable
3. Instance variable
4. Final variable
5. Transient variable
6. Volatile variable

LOCAL VARIABLE:
If the variable is declared within a block (block of method, block of constructor, Block of loop, block of if or else, block of switch or case, or any general block)   it is known as local variable.

class Test{
-------block-------
}

We cannot make the local variable as public
We cannot make the local variable as public , private or protected because the local variable can only be accessed within the same block , but the public access specifier makes the variable global. Thus it is not possible.
In short, no access specifier is used for a local variable.
Private makes it accessible within the same class.
Local variable is only able to access within the same block in which it is declared.

Local variable is not a member of the class or object, so it can't be called by class name or object name. It only accesses directly within the same block. Before using the local variable, that variable should be initialised.

Static variable
If the variable is declared within the class and not within the block, with a static keyword, then it is known as static variable. Static variable is known as class level variable. This type of variable can be called by class_name or object_name, within the same class as well as outside , it's possible to call a static variable within the same class directly.
Static variable creates a single copy for the whole program.

Instance variable.
This is known as object level variable, because Instance Variable only allocates memory at the time of the object creation.
If the variable is within the class and not within the block without any static keyword, it is known as non static or instance variable. Instance variable only can be called by the object name.

Final variable.
If the variable is declared with a "final" keyword, then it is known as final variable.
JAVA never supports any "const" keyword to declare a constant. If the variable is declared with a "final" keyword, then it is constant in JAVA.
Final variable may or may not be static in JAVA, by its better for programmer to declare the Final variable as Static.

Sunday, 28 June 2015

OPERATORS IN JAVA

OPERATORS IN JAVA

JAVA supports 44 number of operators and like C and C++, it's divided into 3 categories ,
1. Unary operator.
2. Binary operator
3. Conditional operator.

UNARY: + , - , ++ , -- , !

BINARY:
Arithmatic: + , - , * , / , ÷
Logical: && , ||
Bitwise: & , | , ^
Relational: > , < , >= , <= , != , ==
Assignment: = , += , -= , *= , ÷= , /=
Shift: << , >> , >>> ( unsigned right shift)

Ternary: ?!
Special: ( ), [ ] , , , . , type , instanceof

Modulus operator never works for float and double value in C or C++, but works in JAVA. I.e System.out.println(10.0℅3.0); o/p = 1.0

TYPE OPERATOR OR TYPE CASTING OPERATOR
Typecasting in JAVA is divided into 3 types-
1. Implicit casting
2. Explicit casting
3. Boolean casting.

Instanceof operator: it's am object comparison operator.
This operator always checks the object existence and returns Boolean value.
Object if the child class can be the object of the parent class.

DOT OPERATOR: It access the variable or the constant, accesses the method , accesses the object, accesses the class and accesses the package.

Saturday, 27 June 2015

FLOAT AND DOUBLE

Float and Double : these two datatypes always stores the real Constant.
Real Constant is always by default double in nature.( example, 4.2 is double in nature and thus, it is a real Constant.)
Thus, float x = 4.2; is an error.
Rather you have to typecast it as : float x = (float)4.2; or float x = 4.2f.

CHARACTER DATATYPE:
Character in JAVA supports Unicode. 65536 number of characters are supported by the JAVA language, since the maximum value of 16bits is 216 which is 65536.
Boolean in JAVA only supports true or false values.

Friday, 26 June 2015

DATATYPE AND WRAPPER CLASS

JAVA supports 8 primitive datatypes, then the language is not purely object oriented.
For each datatype. JAVA provides a predefined class ( known as wrapper class in JAVA).
Wrapper class is used to make JAVA pure object oriented.

All the wrapper class present in java.lang package and the default package in JAVA.

PRIMITIVE DATATYPE

Datatype.    Size.    default value.    Wrapper class
Byte.             8 Bits.           0.                    Byte
Short.            16 Bits.        0.                    Short      
Int.                 32 Bits.         0                    Integer
Long.             64 Bits.          0L.                Long
Float.             32 Bits.          0.0f.             Float
Double.          64 Bits.         0.0.                Double
Boolean.         1 Bit.             False.           Boolean
Char.               16 Bits.        /u0000.         Character

Wrapper class makes JAVA pure object oriented but still it's not purely object oriented ad it supports primitive datatypes.

Signed and unsigned are the two keywords that are not available in JAVA. So by default the datatype is signed in nature. Character datatype in JAVA is signed in nature, I.e it only holds positive value. (As ASCII value us always positive)

Negative number is always stored in the form of 2's complement. Example: 11111111 -> 00000001(2's complement) = -1.

MAX_VALUE & MIN_VALUE are the two predefined static constants which always display the maximum and the minimum value related to the datatype and present in each wrapper class.

toString ( ) is a predefined static method present in the wrapper class which converts any datatype to the string format.
parseByte( ) is a static method present in the Byte upper class which converts a string to byte datatype.

Class 2

Class 2

Void: The main method never returns any value to the JVM. If the main method doesn't return any value, then the return type should be void in JAVA.
public static = static public
COMMAND LINE ARGUMENT
The arguments accepted by the main function is called command line arguments. The main function in JAVA only accepts array of strings as an argument.
String is a predefined class present in JAVA language.
String args [ ] -> name of the array of strings.

NAMING CONVENTION FOR PREDEFINED MEMBERS.
1. Class -
Example: BufferedReader , String
Each word's first letter should be capital, and there shouldn't be any space.
2. Predefined "to" Method -
Example: toString( ), toCharArray( ) ; I.e except the first word, each word's first letter should be capital.
3. Predefined Constant: SIZE , MAX_VALUE; All the letters should be inupper case.
4. Package: java.lang, javax.swing, java.applet ; here each letter should be in lower case and the packages are separated by a "." symbol.

DEFAULT LENGTH OF THE COMMAND LINE ARGUMENT IS 0.

EXAMPLE OF A COMMAND LINE ARGUMENT:
public class Test{
static public void main ( String args[  ] )
{
    System.out.println (args.length); // 2
    for(int i=0; I<args.length; I++)
    {
        System.out.println (args [ i ]); //10, 20
    }
    System.out.println( args[ 0 ] + args [ 1 ] ); // 1020
    System.out.println(10+20); // 30
    System.out.println (5+6+'A'+'B'+5+6+(4+5)); //76B569
    System.out.println("10" + (5 - 10)); // 10-5
}
}

Convert string to integer by parseInt( ). It is a static member and it is called by a class.
Similarly, string to float can be converted by the use of parseFloat.

SYSTEM.OUT.PRINTLN:
A static member is always called by a Class name and a Non Static member is always called by an object name.
The difference between System.out and System.err is that the output produced by System.err can't redirect a file but System.out can
The difference between print and println is that println just adds a new line after printing your desired line.

Thursday, 25 June 2015

INTRODUCTION TO JAVA PROGRAMS

Block----> public class class_name
{
public static void main(string args[ ])
{
System.out.println (...);
}
}

Access specifier in JAVA language:
The major job of access specifier is to specify the scoop of the variable (data member), constructor out any class.
Access specifier---> variable, function, class, constructor.

JAVA folder-> package

Boundaries/scope:
within the same class
• within the same package
• outside the package

Types of access specifier in JAVA: public, protected, default(no access specifier), private

Public: within the same class, within the same package, outside the package
Protected: within the same class, within the same package and outside the package (only in inheritance)
Default: within the same class, within the same package
Private : within the same class.

In JAVA, you can't declare a variable or function or any member as global.

Java doesn't support an global member like C or C++, bit if a member is declared as public , it behaves as a global member in JAVA.

Execute that class that only contains the main.
You can't execute more than one class simultaneously.

If the class is declared as public then the class name and file name should be the same.

Almost one public class can be declared and that would be the entry point.

Private and public keywords are not allowed for top level class but nested class can be declared as private or protected.

Class
Class is a collection of similar kind of objects, that is from one class we can create "n" number of objects, all with similar type.
Class is a blue print of the object, I.e skeleton.

Class is a logical container which never allocated any memory.
In JAVA , class is able to contain 4 types of members.
1. Data mamber
2. Method member
3. Block member
4. Constructor member

public class Test
{
  int x; // data member
  Test() // constructor member
  {
  }
  static // block member
  {
  }
  public static void main( string args[]) //method member
  {
  System.out.println("Hello");
}

Javac: to compile the JAVA program.
Javap: JAVA dessemble. It will display all the members present in all user defined or pre defined class.

Class name is the identifier which maybe same as the file name, but should satisfy the rules of the identifier ( data member, object/ reference name, method name, constructor name, class name, Package name )
Space is not allowed in identifier.
Identifier rules:
1. Space is not allowed in identifier.
2. Digits are allowed but the name can't start with digits.
3. Should not be a reserved word. (Keyword)
4. No special symbols are allowed except "_" or "$"

PUBLIC STATIC VOID MAIN
In JAVA, "main" is user defined function, but the signature is predefined.
Compiler never checks the "main" method existence.
But the execution is always started from the main method.

KERNEL calls the main method in C and C++
But JVM( JAVA Virtual Machine ) calls the main function in JAVA.
• JAVA Virtual Machine.
• System software
• Development in C language.
• Platform dependant ( but JAVA is platform independant)

JVM is a system software inbuilt to OS (I.e itbis present by default)
The main job of JVM is to execute the class file, and the execution always starts from the main method.l, as the main method is always called by JVM from the OS, which is outside the package. So main method should be declared as public, else it can't be called from outside.

WHY MAIN METHOD IS STATIC IN JAVA?
Main method is called by the JVM with the name of the class.
If a method is called by the class name, it should be declared as static in JAVA.

JAVA BASICS

Total number of keywords in the current version is 48.
The latest keyword is enum.
JDK: JAVA Development kit.
JRK: JAVA runtime environment
JIT: JAVA intime compiler
any language that supports primitive data type (not exactly objects) is called primitive object oriented.
It doesn't support multiple inheritance directly with the help of class.
Since it doesn't support overloading, it is partial object oriented.
Why do we choose JAVA language?
1. Open source: whose source code is open for the user.
     Closed source: (Microsoft) source code can be accessed by the corporation.
2. Platform independent: platform independent means that the program which can run in different platforms.
Example: Linux, windows.
Stage after compilation where the application of generated after the compilation, and can be run in different operating systems.
If a language supports platform independent application, the language is called platform independent programming language.
An application executable in any environment is called platform independent application.
3. Automatic memory management (jvm software)
Example: for malloc we use free to deallocate the memory.
Memory allocation and deallocation is done by the software itself.
4. Garbage collection: it collects the garbage values and gives some default values or error message to the user.
5. Object oriented: JAVA is nearly pure object oriented. Though it doesn't support all the feature of oops , but supports maximum of them.
There is reusability, that's why object oriented.
6. Portability: JAVA is portable. It is developed in an environment such that it can be portable to another environment.
7. Direct support distributed application: JAVA is multi-threaded.
8. Simple,: 80,000 predefined classes are present.
9. Multi threaded.
10. Secure.
11. Exception handling
12. Robust