Java

Java Datatypes

What is a Datatype?
A data type is an attribute of data that tells the compiler or interpreter how the programmer intends to use the data.

Data types in Java programming language

There are two different sets of data types in Java

  • Primitive Datatypes
  • Non Primitive Datatypes

The below image depicts different data types in java

Java Datatypes

The Java programming language is statically-typed, which means that all variables must first be declared before they can be used. This involves stating the variable’s type and name.

boolean: is used to store true or false value.

byte, short, int, long: is used to store whole numbers.

float, double: is used to store fractional numbers.

The below table depicts different datatypes and the size of those datatypes.

package com.test.java;

public class BasicDataTypes {
 
 static byte i = 127;
 static short j = 3456;
 static int k = 24233534;
 static long l = 34564;
 static float fl = 35325432;
 static double db = 34523763;
 static boolean b = true;
 static char c = 'c';
 
 static String testString = "Hello World";
 
 
 public static void main(String [] args) {
  char[] testCharString = new char[11];
  testCharString[0] = 'h';
  testCharString[1] = 'e';
  testCharString[2] = 'l';
  testCharString[3] = 'l';
  testCharString[4] = 'o';
  testCharString[5] = ' ';
  testCharString[6] = 'w';
  testCharString[7] = 'o';
  testCharString[8] = 'r';
  testCharString[9] = 'l';
  testCharString[10] = 'd';
  
  System.out.println("Default value of byte is ---> "+i);
  System.out.println("Default value of short is ---> "+j);
  System.out.println("Default value of int is ---> "+k);
  System.out.println("Default value of long is ---> "+l);
  System.out.println("Default value of float is ---> "+fl);
  System.out.println("Default value of double is ---> "+db);
  System.out.println("Default value of boolean is ---> "+b);
  System.out.println("Default value of char is ---> "+c);
  System.out.println("Default value of String is ---> "+testString);
  System.out.println("Default value of char is ---> "+String.valueOf(testCharString));

  
 }

}

Here is the video

rajendra

Share
Published by
rajendra
Tags: Java

Recent Posts

Largest Unique Number Java Solution

Question : Given an array of integers A, return the largest integer that only occurs once.…

9 months ago

Jump Search Algorithm In Java

Jump search algorithm is a pretty new algorithm to search for an element in a…

1 year ago

Knuth Morris Pratt Pattern Search Algorithm

What is Knuth Morris Pratt or KMP algorithm ? KMP is an algorithm which is…

1 year ago

Binary Search Algorithm In Java

Binary Search is a Logarithmic search which finds the target element in a sorted array…

1 year ago

Leetcode Integer to Roman Java Solution

Roman numerals are represented by seven different symbols: I, V, X, L, C, D and M. Symbol Value I 1 V 5 X…

2 years ago

Leetcode Container With Most Water Java Solution

Given n non-negative integers a1, a2, ..., an , where each represents a point at coordinate (i, ai). n vertical lines are drawn such…

2 years ago

This website uses cookies.