Object Creation and Data types in JAVA

 Object Creation and Data Types in JAVA



+ In JAVA,we can generate an object if only there is a class.

+ Object is created from a class.

+ And also only a class can be converted into an object.

+ Everything in JAVA is based on objects and classes along with its arrtibutes and methods.

+ Let's see how to create an object step by step.


👉 Create a Class


public class Main{

   int x=5;

}


👉 Create an Object


public class Main{

   int x=5;

   public static void main(string[]arg) {

        Main obj = new Main();

        System.out.println(obj.x);

   }

}

Data Types

Formats or types that are being used to keep data are "Data Types".

In JAVA,a variable must be written with a specified data type that is suitable to it.

Data types are divided into 2 parts.

(i) Primitive data types

  • byte(integer)
  • char(integer)
  • short(integer)
  • int(integer)
  • long(integer)
  • float(floating point)
  • double(floating point)
  • boolean(binary-true/false)

(ii) Non-Primitive data types

  • String
  • Classes
  • Arrays

If the relevent primitive types are same type,we can access them.

If they are different,we cannot access them directly.

eg :- int a=5;

      long b=7;

      a+b ;

Any arithmetic operation can be performed )

eg :- long c=12345;

      float d=786.98;

      c+d ;

( As they are in different types,they cannot be accessed )



String

String is a prebuilt class in JAVA.
It is unchangeable.
It is used as a data type.
String is not a primitive data type.
Any data can be assigned with String.
String value can be given inside commas.

eg:- String s1="1234567" ;
     String s2="$#%&^***" ;
     String s3="hello!" ;

reference

Comments

Popular posts from this blog

Variable and Method types in JAVA

Polymorphism

Casting