Objects And Strings In Java

About Objects

  • Object in Java is an instance of a Java class.
    • When you create a variable and point it at an object. The variable isn’t the object itself, instead it is just a reference to the object
  • Non-primitive variables are references to objects.
  • Objects can have multiple references.
    • That is, they point to single object.
    • For instance, in memory some values assigned to that object changes. This value change is reflected in all of its references.

Example

public class ObjectInJavaExample {    	public String color;    	public static void main( String[] args ){  		ObjectInJavaExample candy = new ObjectInJavaExample();  		candy.color = "Red";  		candy.displayCandy();  	}    	private void displayCandy() {  		System.out.println("Color of candy is: " +this.color);  	}    }

Output: Color of candy is: Red

Notes:

  • The String variable does not have static. This means it is an instance varaible or field.
  • An instance variable is not a member of the class itself. It is member of an instance of class.
  • In main method, candy is an instance of ObjectInJavaExample class. The data type is name of class.
  • ObjectInJavaExample() is a call to constructor method.
  • ObjectInJavaExample candy = new ObjectInJavaExample(); line is called as instantiation
  • Now once the instance is declared, the variables of instance are allocated memory.
  • Class instances or objects can also have instance methods. displayCandy() is an instance method.
  • this keyword helps to output information of the instance of class which called the method.

String Is an Object In Java

  • The String class, just like the helper classes of primitive data types, is member of java.lang.String.
  • Example:
    • String str = new String(“Hello World!”);  //This creates a string variable
  • String class is special because you can use a shortcut too:
    • String str1 = “Hello World!”;  //This is exactly same as before
  • String is basically an array of characters. However, character is a primitive and string is a complex object.
  • String can accommodate as many characters as you want till the memory is available.
  • String objects are immutable.
    • That is, once you instantiate and assign a value, you can’t change the value.
    • To us, it looks like we are changing the value of same object.
    • However, what happens in back-end is de-referencing.
    • Example:
      • String str1 = “Okay”;
      • str1 = “Not Okay”;  //De-referencing
    • The old object with “Okay” is assigned for garbage collection.
    • Thus, we create a brand new object with the new value.

Character Array to String

public class CharArrayToString {    	public static void main( String[] args ){  		char[] charArr = {'O', 'k','a','y'};  		String str = new String(charArr);  //using String constructor  		System.out.println(str);  	}  }

String to Character Array

public class StringToCharArray {    	public static void main( String[] args ){  		String str = "Hello World!";  		char[] charResult = str.toCharArray();  		for(char c : charResult){  			System.out.print(c);  		}  	}  }