Software Development Exam  >  Software Development Notes  >  Basics of Java  >  Java Data Types

Java Data Types | Basics of Java - Software Development PDF Download

When programming in Java, it is essential to understand data types as they determine the kind of data that can be stored in a variable. Java offers a rich set of built-in data types to cater to various needs. In this article, we will explore the different data types in Java, along with examples and code explanations, to help beginners grasp the concepts effectively.

Primitive Data Types

Java provides eight primitive data types, which are the basic building blocks for storing simple values. These data types include:
a. byte

The byte data type stores whole numbers from -128 to 127. It occupies 8 bits of memory.

byte age = 25;

System.out.println(age); // Output: 25

b. short

The short data type stores whole numbers from -32,768 to 32,767. It occupies 16 bits of memory.

short population = 30000;

System.out.println(population); // Output: 30000

c. int

The int data type stores whole numbers from -2,147,483,648 to 2,147,483,647. It occupies 32 bits of memory.

d. long

The long data type stores whole numbers from -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807. It occupies 64 bits of memory.

long totalSeconds = 9876543210L;

System.out.println(totalSeconds); // Output: 9876543210

Note: To indicate a long literal, suffix it with an 'L' (e.g., 9876543210L).

e. float
The float data type stores decimal numbers with single-precision. It occupies 32 bits of memory.

float pi = 3.14f;

System.out.println(pi); // Output: 3.14

Note: To indicate a float literal, suffix it with an 'f' (e.g., 3.14f).

f. double
The double data type stores decimal numbers with double-precision. It occupies 64 bits of memory.

double price = 9.99;

System.out.println(price); // Output: 9.99

g. boolean
The boolean data type stores either true or false values. It is commonly used in conditional statements and loops.

boolean isActive = true;

System.out.println(isActive); // Output: true

h. char
The char data type stores a single character. It occupies 16 bits of memory and uses single quotes to denote the character value.

char grade = 'A';

System.out.println(grade); // Output: A

Reference Data Types


In addition to the primitive data types, Java also provides reference data types, which are more complex and capable of storing collections of values or objects. Some commonly used reference data types include:
a. String
The String data type stores a sequence of characters. It is not a primitive type but a class provided by Java. Strings are immutable, meaning they cannot be changed after they are created.

String message = "Hello, world!";

System.out.println(message); // Output: Hello, world!

b. Array

An Array is a data structure that stores a fixed-size sequential collection of elements of the same type. Arrays can store elements of any data type, including primitive and reference types.

int[] numbers = {1, 2, 3, 4, 5};

System.out.println(numbers[2]); // Output: 3

c. Class
The Class data type is used to represent a class in Java. It allows you to define and create objects of that class.

class Person {

  String name;

  int age;

}


Person john = new Person();

john.name = "John Doe";

john.age = 30;

System.out.println(john.name); // Output: John Doe

Sample Problems

Now, let's solve a few problems to reinforce our understanding of Java data types:

Problem 1: Write a program to calculate the area of a circle given its radius (double data type).

double radius = 5.0;

double area = Math.PI * Math.pow(radius, 2);

System.out.println(area); // Output: 78.53981633974483

Problem 2: Write a program to check if a given number is even (int data type).

int number = 8;

boolean isEven = number % 2 == 0;

System.out.println(isEven); // Output: true

Problem 3: Write a program to concatenate two strings (String data type).

String firstName = "John";

String lastName = "Doe";

String fullName = firstName + " " + lastName;

System.out.println(fullName); // Output: John Doe

By understanding and utilizing the various data types in Java, you can efficiently work with different types of data and build robust programs. In this article, we covered the primitive data types (byte, short, int, long, float, double, boolean, and char) as well as reference data types (String, Array, and Class). We also solved sample problems to apply the concepts in practical scenarios. With this knowledge, you can start exploring more complex programming concepts in Java.

The document Java Data Types | Basics of Java - Software Development is a part of the Software Development Course Basics of Java.
All you need of Software Development at this link: Software Development
60 videos|37 docs|12 tests

Top Courses for Software Development

60 videos|37 docs|12 tests
Download as PDF
Explore Courses for Software Development exam

Top Courses for Software Development

Signup for Free!
Signup to see your scores go up within 7 days! Learn & Practice with 1000+ FREE Notes, Videos & Tests.
10M+ students study on EduRev
Related Searches

ppt

,

Extra Questions

,

Sample Paper

,

Exam

,

practice quizzes

,

Free

,

Semester Notes

,

video lectures

,

Objective type Questions

,

study material

,

Summary

,

Important questions

,

Previous Year Questions with Solutions

,

mock tests for examination

,

Java Data Types | Basics of Java - Software Development

,

past year papers

,

pdf

,

MCQs

,

Java Data Types | Basics of Java - Software Development

,

shortcuts and tricks

,

Viva Questions

,

Java Data Types | Basics of Java - Software Development

;