Skip to content

Crack SDE

Most of the content are generated by AI, with human being reviewed, edited, and revised

Menu
  • Home
  • Daily English Story
  • Tech Interviews
  • Cloud Native
  • DevOps
  • Artificial Intelligence
Menu

Java tutorial – 2 data types

Posted on 05/31/202305/31/2023 by user

Java Data Types

Primitive Data Type 

In Java, there are two types of data types: Primitive data type and Non-primitive data type. 

Primitive Data Types: These are the built-in data types in Java.

  1.    – byte : 8-bit signed integer. It has a minimum value of -128 and a maximum value of 127 (inclusive).
  2.    – short : 16-bit signed integer. It has a minimum value of -32,768 and a maximum value of 32,767 (inclusive).
  3.    – int : 32-bit signed integer. It has a minimum value of -2,147,483,648 and a maximum value of 2,147,483,647 (inclusive).
  4.    – long: 64-bit signed integer. It has a minimum value of -9,223,372,036,854,775,808 and a maximum value of 9,223,372,036,854,775,807 (inclusive).
  5.    – float: Single-precision 32-bit IEEE 754 floating point.
  6.    – `double`: Double-precision 64-bit IEEE 754 floating point.
  7.    – boolean: The boolean data type has only two possible values: true and false.
  8.    – `char`: Single 16-bit Unicode character. It has a minimum value of ‘\u0000’ (or 0) and a maximum value of ‘\uffff’ (or 65,535 inclusive).
Data TypeSize(in byte)Description
byte1-2^7 to 2^7-1, -128 to 127 
short2-2^15 to 2^15-1, -32,768 to 32,767
int4-2,147,483,648 to 2,147,483,647
long8-2^63 to 2^63-1, -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807
float4Sufficient for storing 6 to 7 decimal digits
double8Sufficient for storing 15 decimal digits
boolean1Stores true or false values
char2Stores a single character/letter or ASCII values

Declaration of Primitive Data Types and Assign Values  in Java:

byte x = 10; 
short num1=10;
int myNum = 5;               
float myFloatNum = 5.99f;    
double myDoubleNum = 5.5;
boolean myBool = true;       
char myLetter = 'D';   

Non-Primitive Data Type

Non-Primitive Data Types (Reference Data Types, or reference types): These are created by the programmer and are not defined by Java (except for String). Non-primitive types can be used to call methods to perform certain operations, while primitive types cannot. Examples of non-primitive types are:

   – String: The most commonly used non-primitive data type. It represents a sequence of characters.

   – Arrays: A group of like-typed variables that are referred to by a common name.

   – Classes: A class is a blueprint from which individual objects are created.

   – `Interfaces`: Like a class, an interface can have methods and variables, but the methods declared in an interface are by default abstract (only method signature, no body).

   – Enum: A special data type that enables for a variable to be a set of predefined constants. The variable must be equal to one of the values that have been predefined for it.

String myText = "Hello";     // String
String[] array = new String[100]; // Array

// Interface
interface Animal {
public void animalSound(); // interface method (does not have a body)
public void sleep();
}

// Enumerate typeenum Level {
  LOW,
  MEDIUM,
  HIGH
}

Note: Java is statically typed. That means each variable must be declared with a data type before it can be used.

Differences Between Two Data Types

The main difference between primitive and non-primitive data types are:

  • Primitive types are predefined (already defined) in Java. Non-primitive types are created by the programmer and are not defined by Java (except for String).
  • Non-primitive types can be used to call methods to perform certain operations, while primitive types cannot.
  • A primitive type has always a value, while non-primitive types can be null.
  • A primitive type starts with a lowercase letter, while non-primitive type starts with an uppercase letter.
  • The size of a primitive type depends on the data type, while non-primitive types have all the same size.

Java Wrapper Classes

Wrapper classes provide a way to use primitive data types (int, boolean, etc..) as objects.

The table below shows the primitive type and the equivalent wrapper class:

Primitive Data TypeWrapper Class
byteByte
shortShort
intInteger
longLong
floatFloat
doubleDouble
booleanBoolean
charCharacter

Pass-by-reference and Pass-by-value

  • Pass-by-reference: When a method is called, the method arguments reference the same variable in memory as the caller. 
  • Pass-by-value: When a method is called, the caller passes a copy of the argument variables to the method resulting in two values in memory.

Java is always pass-by-value. The reason this is confusing is that Java achieves its functionality using special variable types known as object references. 

Reference: https://sentry.io/answers/java-pass-by-reference-or-value/ 

Header image from: https://getkt.com/blog/reintroduction-to-java-data-types/

Share this:

  • Click to share on Facebook (Opens in new window) Facebook
  • Click to share on X (Opens in new window) X

Related

Recent Posts

  • LC#622 Design Circular Queue
  • Started with OpenTelemetry in Go
  • How Prometheus scrap works, and how to find the target node and get the metrics files
  • How to collect metrics of container, pods, node and cluster in k8s?
  • LC#200 island problem

Recent Comments

  1. another user on A Journey of Resilience

Archives

  • May 2025
  • April 2025
  • February 2025
  • July 2024
  • April 2024
  • January 2024
  • December 2023
  • November 2023
  • October 2023
  • September 2023
  • August 2023
  • June 2023
  • May 2023

Categories

  • Artificial Intelligence
  • Cloud Computing
  • Cloud Native
  • Daily English Story
  • Database
  • DevOps
  • Golang
  • Java
  • Leetcode
  • Startups
  • Tech Interviews
©2025 Crack SDE | Design: Newspaperly WordPress Theme
Manage Cookie Consent
To provide the best experiences, we use technologies like cookies to store and/or access device information. Consenting to these technologies will allow us to process data such as browsing behavior or unique IDs on this site. Not consenting or withdrawing consent, may adversely affect certain features and functions.
Functional Always active
The technical storage or access is strictly necessary for the legitimate purpose of enabling the use of a specific service explicitly requested by the subscriber or user, or for the sole purpose of carrying out the transmission of a communication over an electronic communications network.
Preferences
The technical storage or access is necessary for the legitimate purpose of storing preferences that are not requested by the subscriber or user.
Statistics
The technical storage or access that is used exclusively for statistical purposes. The technical storage or access that is used exclusively for anonymous statistical purposes. Without a subpoena, voluntary compliance on the part of your Internet Service Provider, or additional records from a third party, information stored or retrieved for this purpose alone cannot usually be used to identify you.
Marketing
The technical storage or access is required to create user profiles to send advertising, or to track the user on a website or across several websites for similar marketing purposes.
Manage options Manage services Manage {vendor_count} vendors Read more about these purposes
View preferences
{title} {title} {title}