Primitive and non-Primitive Data types in Java

Data Type: Java Data types of data which is used in the java program. There are many predefined data types in java library like character data type, integer data type and floating data type.

Data Types specify the type of data that cab be stored inside a variable in java. Java Language is a statically –typed language. This means that all variables must be declared before they can be used data types.

int data

Here data is a variable name and the data type of the variable is int. integer data type can be stored only integer value in java.


Java Data Types Hackerrank Solution
Java Data Types Hacker rank Solution

      Primitive Data Types in java

      Primitive Data Type: Primitive data type which is predefined in java library is called primitive data type. It is named by a keyword integer, float or character data type. Primitive data types are 8 types in java. Byte, Short, int, long, float double boolean and char.

      int: This data type is used to represent integer values. It can hold values from -2147483648 to 2147483647.

      2. long: This data type is used to represent long integer values. It can hold values from -9223372036854775808 to 9223372036854775807.

      3. short: This data type is used to represent short integer values. It can hold values from -32768 to 32767.

      4. byte: This data type is used to represent byte values. It can hold values from -128 to 127.

      5. float: This data type is used to represent single-precision floating-point values. It is generally used to represent decimal values.

      6. double: This data type is used to represent double-precision floating-point values. It is generally used to represent decimal values with higher precision.

      7. boolean: This data type is used to represent boolean values (true or false).

      8. char: This data type is used to represent a single character.

      Integer Data Type in Java

      Data TypeSize in bytesRange
      byte1-128 To 127
      short2-32768 To 32767
      int4-2147483648 To 2147483648
      long8-9223372036854775808

      Float Data Type in Java

      Data Type

      Size in bytes

      Range

      float

      4

      -3.4e-038 To +3.4e-038

      double

      8

      1.7e-308 To 1.7e+038

      Boolean Data Type in Java

      Keywordboolean
      Syntaxboolean a;
      ValueTrue and False
      DefaultFalse

      Character Data Type in Java

      Keywordchar
      Syntaxchar a;
      Value'a','@','9'
      Default0 To 65536

      Boolean Data Type in Java 

      1. Boolean Data Types: The Boolean data type has two possible values, like true and false. Boolean data type can be used for true and false condition. Boolean data type default value false.

      class Bool


      {


      public static void main(String args[])


      boolean data=true;


      System.out.println(data);


      }


      }



      **********OUTPUT**********


      true

      When we use Byte Data Type in Java

      2. Byte Data Type: Byte Data Type can be hold the value of a variable will be within -128 to 127. It is used to instead of int to save memory. byte data type default value is 0.

      class Bytesd


      {


      public static void main(String args[])


      byte data;


      data =430;


      System.out.println(“Value of Data:”+data);


      }


      }


      **********OUTPUT**********


      Value of Data:430

      Short Data Type in Java Example

      3. Short Data Types: short data types can be stored value -32768 to 32768. Short data type can be used to integer data type or long data type. Short data type default value 0.

      class Bytesd


      {


      public static void main(String args[])


      byte data;


      data =430;


      System.out.println(“Value of Data:”+data);


      }


      }


      **********OUTPUT**********


      Value of Data:430


      Int Data Type Range in Java

      4. int Data Type: int data type can have values -2147483648 to 2147483648 bits signed two complement integer values. if you are using java 8 or later, you can use an unsigned 32-bit integer value. int data type have minimum value of 0 and maximum value (2)32-1. integer data type default value 0.

      class Integerdata


      {


      public static void main(String args[])


      int data=-564536;


      System.out.println(“Value of Data:”+data);


      }


      }

      **********OUTPUT**********


      Value of Data:-564536

      Long Data Type in Java Example

      5. Long Data Type: Long Data Type can be store values from (-2)63 to 2(62)-1 can be using 64-bit signed two complement integer values. long data types can be used, default value is 0.

      class Longdata


      {


      public static void main(String args[])


      long int data=6564536;


      System.out.println(“Value of Data:”+data);


      }


      }

      **********OUTPUT**********


      Value of Data:6564536


      Double Data Type in Java Example

      6. Double Data Type: Double data type can be used in 64 bit floating point number. Floating data type can be stored floating values.

      class Doubledata


      {


      public static void main(String args[])


      double float data=656.45d;


      System.out.println(“Value of Data:”+data);


      }


      }

      **********OUTPUT**********


      Value of Data:656.45

      Float Data Type in Java Example

      7. Float Data Type: Float data type used for single precision 32 bit floating point values. float data type is used only floating point values in java.

      class Floatdata


      {


      public static void main(String args[])


      float data=65.45f;


      System.out.println(“Value of Data:”+data);


      }


      }

      **********OUTPUT**********


      Value of Data:65.45

      Character Data Type Example

      8. Char Data Type: Char data type is used in character values. char data type is used single character in java language.

      class Chardata


      {


      public static void main(String args[])


      char letter='R';


      System.out.println(“Value of Data:”+letter);


      }


      }

      **********OUTPUT**********


      Value of Data:R

      Non-Primitive Data Types in Java

      Non-Primitive Data Type: A data type which is derived from a primitive data type is called a non-primitive data type. It is also called reference data type in java. derived data types array, class interface and more.

      Reference data types

      String: This data type is used to represent a sequence of characters. It is not a primitive data type, but it is treated as a special data type in Java.

      Class: This data type is used to represent a user-defined class.

      Array: This data type is used to represent a collection of elements of the same data type.

      Interface: This data type is used to define a set of related methods that a class can implement.

      In addition to these data types, Java also provides support for type casting, which allows you to convert a value from one data type to another. For example, you can cast a float value to an int value by using the (int) type cast operator.

      What is constant in java

      Constant: Constant is an element of java program which values can not be changed at a time of execution of java program is called constant. constant is int, float and character data types.

      what is an integer constant write integer forming rule of java

      1. integer constant is used to at least one digit.
      2. integer constant is used to must not have a decimal point number.
      3. integer constant is used to positive or negative values.
      4. integer constant can not, no comma or blank space are allowed in integer constant.

      How to Declare Float in Java

      1. float constant is used to at least one digit.
      2. float constant is used to must have a decimal point number.
      3. float constant is used to positive or negative values.
      4. float constant can not, no comma or blank space are allowed in float constant.

      What is Character Constant in Java

      1. character constant is used to single alphabet digit or special symbol used in java.
      2. character constant length of character constant is 1 character.
      3. character constant is used to enclosed within single quotes. like (char c='R').

      FAQ Java Data Types

      Q.1 What are non-primitive data types in java.
      Ans: Non-primitive data types is a derived data types in java language. Java Data types is the most useful java programming language. non-primitive data type can be used derived data type like It is also called reference data type in java. derived data types array, class interface.

      Q.2 which of the following are primitive data types in java.
      Ans: 
      1. Boolean data type
      2. Byte data type
      3. Char data type
      4. Short data type
      5. Int data type
      6. Long data type
      7. Float data type
      8. Double data type

      Q.3 What are the 5 main data types.
      Ans:
      1. Integer
      2. Float
      3. Character
      4. Long double
      5. String

      Q.4. What are the 4 OOP in Java?
      Ans: In object-oriented programming (OOP), there are four main principles or concepts that are used to design and structure code:

      1. Encapsulation: This refers to the idea of bundling data and methods that operate on that data, within a single unit, or object. Encapsulation helps to protect the data from external access and modification, and it allows the object to be self-contained and self-sufficient.

      2. Abstraction: This refers to the idea of exposing only the necessary details of an object to the outside world, while hiding the internal implementation details. Abstraction helps to simplify the interface of an object and make it easier to use.

      3. Inheritance: This refers to the ability of a class to inherit properties and behaviors from a parent class. Inheritance allows you to create a new class that is a modified version of an existing class, without having to rewrite all the code in the new class.

      4. Polymorphism: This refers to the ability of a single interface to be used to perform different tasks. Polymorphism allows you to write code that can be applied to multiple different types of objects, without having to know exactly what type of object it is working with.

      In Java, these principles are supported through the use of classes, objects, and various language constructs, such as inheritance, interfaces, and polymorphism. Together, these concepts form the foundation of OOP in Java and help to make it a powerful and flexible programming paradigm