Java Data Types šŸŽÆ

beginner
20 min

Java Data Types šŸŽÆ

Welcome to our comprehensive guide on Java Data Types! This lesson is designed to help you understand the different types of data that can be stored and manipulated in Java. By the end of this tutorial, you'll have a solid foundation in working with data in Java. Let's get started!

Understanding Data Types šŸ“

In programming, a data type is a classification that specifies the type of data a variable can hold. In Java, there are two main categories of data types: Primitive and Non-Primitive.

Primitive Data Types šŸ’”

Primitive data types are built-in data types that are directly supported by Java. Here are the eight primitive data types:

  1. byte: Signed 8-bit integer, range: -128 to 127
  2. short: Signed 16-bit integer, range: -32,768 to 32,767
  3. int: Signed 32-bit integer, range: -2,147,483,648 to 2,147,483,647
  4. long: Signed 64-bit integer, range: -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807
  5. float: Single-precision floating-point number, range: approximately -3.402823E+38 to 3.402823E+38
  6. double: Double-precision floating-point number, range: approximately -1.79769313486231E+308 to 1.79769313486231E+308
  7. boolean: A value that can be either true or false
  8. char: Unicode character, range: U+0000 to U+FFFF

Declaring Variables šŸ’”

To declare a variable in Java, you use the variable name, data type, and the = operator. Here's an example of declaring variables for different data types:

java
byte myByte = 10; short myShort = 20; int myInt = 30; long myLong = 40L; // Long should be declared with L at the end float myFloat = 50.0f; // Float should be declared with f at the end double myDouble = 60.0; boolean isTrue = true; char myChar = 'A';

šŸ’” Pro Tip: When using a single character for a char variable, it must be enclosed in single quotes (').

Working with Variables šŸ’”

Now that you've learned how to declare variables, let's see how to work with them. Here's a simple example of printing variables using the System.out.println() method:

java
public class Main { public static void main(String[] args) { byte myByte = 10; short myShort = 20; int myInt = 30; long myLong = 40L; float myFloat = 50.0f; double myDouble = 60.0; boolean isTrue = true; char myChar = 'A'; System.out.println("myByte: " + myByte); System.out.println("myShort: " + myShort); System.out.println("myInt: " + myInt); System.out.println("myLong: " + myLong); System.out.println("myFloat: " + myFloat); System.out.println("myDouble: " + myDouble); System.out.println("isTrue: " + isTrue); System.out.println("myChar: " + myChar); } }

This code declares variables for all eight primitive data types and prints them out. When you run this code, you'll see the output in your console.

Practice Time šŸŽÆ

Now that you've learned the basics, let's test your knowledge with some quiz questions:

Quick Quiz
Question 1 of 1

Which primitive data type can store floating-point numbers with single precision?

Quick Quiz
Question 1 of 1

What is the range of the `char` data type in Java?

That's it for this lesson! In the next lesson, we'll dive deeper into Java data types, explore operators, and learn how to perform basic arithmetic operations. Happy coding! šŸš€