Monday, 26 June 2017

What is String class

1) String is part of java.lang package. 2) String is a group of characters. "Hello" Important Point1: 3) String is immutable. Non changeable. If we create any string object, we can't modify it's value. But if we try to make any changes, new string will be created with new changes. Ex: String str = new String("Hello"); //Object1: "Hello" str.concat(" World!");//Object2: "Hello World!" no reference variable. eligible for garbage collection. System.out.println(str);//Print object1 StringBuffer is another class from java.lang package. Mutable: We can perform any change in the existing object. 4) StringBuffer stringBuffer = new StringBuffer("Hello");//Object1 stringBuffer.append(" World!");//Object1 stringBuffer reference variable pointing to "Hello World!" System.out.println(stringBuffer);//Print object1 "Hello World!" Important Point2: 5) String str1=new String("Hello"); String str2=new String("Hello"); System.out.println(str1 == str2); //False Reference comparison System.out.println(str1.equals(str2))//True Content comparison; 6) StringBuffer str1=new StringBuffer("Hello"); StringBuffer str2=new StringBuffer("Hello"); System.out.println(str1 == str2); //False Reference comparison System.out.println(str1.equals(str2))//False Reference comparison; Important Point3: Two ways to create string: String str1=new String("Hello"); //Two Objects will be created. one in HEAP and one in String common pool String str2="Hello"; //one object will be created in SCP JVM will check with the required content object is already there in SCP or not. if it's missing then only it makes entry in scp. GC is not allowed to access SCP. All entries will be removed only when JVM shutdown. SCP resides in method area. Important Point4: String str1=new String("Hello"); String str2=new String("Hello"); String str3="Hello"; String str4="Hello"; Only one entry in SCP. Two objects in heap. String str = new String("Spring");//Heap: Object(Spring) SCP: Spring str.concat("Summer");//Heap: Object(Spring),Object(Spring Summer) SCP: Spring,Summer String str2 = str.concat("Winter");//Heap: str->Object(Spring),Object(Spring Summer),str2->Object(Spring Winter) SCP: Spring,Summer,Winter str = str.concat(" Fall");//Heap: Object(Spring),str->Object(Spring Fall),Object(Spring Summer),str2->Object(Spring Winter) SCP: Spring,Summer,Winter,Fall

Saturday, 24 June 2017

Java class skeleton and basic definations

Source File: filename.java to compile: javac filename.java -> after compilation filename.class [byte code] to execute: java filename Package Statement[optional] //syntax: package array import statements;[optional] //only when we are importing class from diff //package //Class Declaration: [Access Modifiers(public/default)] [Non Access Modiferes(Abstract/final)] class ClassName extends super class/implements interface { //Variables Section -> Reference variables(classname)/Primitive //variables(int,char,long,double,float,short,boolean and byte) 1) //instance Variables -> [Access modifiers(public/private/protected/default)] [final] type variableName; 2) //class variables/static variables just like instance variables but with static word : one common copy for all objects [Access Modifers] static [final] type variableName; //Constructor -> Method with No return type, class name as method name [AccessModifers(public/default/private/protected)]ClassName(type parameterName) { //initialize instance variables } //Instance Methods [AccessModifers(public/private/default/protected)] [final/abstract/synchronized] returntype methodName(type parameterName) { //logic } //Class method/static method: object is not required [AccessModifers(public/private/default/protected)] static returntype methodName(type parameterName) { //logic } // method ends }//class ends Object Creation: ClassName referenceVariableName = new ClassName([parameters]); // single line comments /*multiple line comments */ A Java program is mostly a collection of objects talking to other objects by invoking each other's methods. Every object is of a certain type, and that type is defined by a class or an interface. Most Java programs use a collection of objects of many different types. Class: A template that describes the kinds of state and behavior that objects of its type support. ■ Object At runtime, when the Java Virtual Machine (JVM) encounters the new keyword, it will use the appropriate class to make an object which is an instance of that class. That object will have its own state, and access to all of the behaviors defined by its class. ■ State (instance variables) Each object (instance of a class) will have its own unique set of instance variables as defined in the class. Collectively, the values assigned to an object's instance variables make up the object's state. ■ Behavior (methods) When a programmer creates a class, she creates meth- ods for that class. Methods are where the class' logic is stored. Methods are where the real work gets done. They are where algorithms get executed, and data gets manipulated.