Java Tutorials
Java Methods
Java Classes
Java File Handling
As explained in the previous chapter, the exception to Java should be the type of data specified:
Data types are divided into two groups:
byte
, short
, int
, long
, float
, double
, boolean
and char
Older data type specifies different size and type of values, and has no additional options.
There are eight types of older data in Java:
Data Type | Size | Description |
---|---|---|
byte | 1 byte | Stores whole numbers from -128 to 127 |
short | 2 bytes | Stores whole numbers from -32,768 to 32,767 |
int | 4 bytes | Stores whole numbers from -2,147,483,648 to 2,147,483,647 |
long | 8 bytes | Stores whole numbers from -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807 |
float | 4 bytes | Stores fractional numbers. Sufficient for storing 6 to 7 decimal digits |
double | 8 bytes | Stores fractional numbers. Sufficient for storing 15 decimal digits |
boolean | 1 bit | Stores true or false values |
char | 2 bytes | Stores a single character/letter or ASCII values |
The first number types are divided into two groups:
Integer types store whole, positive or negative numbers (such as 123 or -456), without decimals. Valid types are byte
, short
, int
and long
. Which type to use, depends on the number of digits.
Floating point types represent numbers with a fraction, consisting of one or more decimal. There are two types: float
and double
.
Even though there are many types of numbers in Java, the most commonly used numbers are int
(whole numbers) and double
(floating point numbers). However, we will explain them all as you read.
Byte
data type can store integers from 128 to 127. This can be used instead of int
or other integer types to save memory if you are sure the value will be between 128 and 127:
byte myNum = 100;
System.out.println(myNum);
The short
data type can store whole numbers from 32768 to 32767:
short myNum = 5000;
System.out.println(myNum);
The int
data type can store absolute numbers from -2147483648 to 2147483647. Generally, and in our study, the int
data type is the preferred data type when we create variables in numerical values.
int myNum = 100000;
System.out.println(myNum);
Long
data types can store complete numbers from -9223372036854775808 to 9223372036854775807. This is used when the int is not large enough to store the value. Note that you must complete the value in "L":
long myNum = 15000000000L;
System.out.println(myNum);
You should use a floating point type whenever you need a decimal number, such as 9.99 or 3.14515.
Floating
data type can store partial numbers from 3.4e − 038 to 3.4e + 038. Note that you must complete the value with "f":
float myNum = 5.75f;
System.out.println(myNum);
Double
data type can store partial numbers from 1.7e − 308 to 1.7e + 308. Note that you must complete a value with "d":
double myNum = 19.99d;
System.out.println(myNum);
Use to float
or double?
The accuracy of the floating point value indicates how many digits the value can have after the decimal point. Float
accuracy is only six or seven digits, while double
variables have an accuracy of about 15 digits. It is therefore safe to use duplicates in most calculations.
A floating point number can also be a scientific number with an "e" to indicate a power of 10:
float f1 = 35e3f;
double d1 = 12E4d;
System.out.println(f1);
System.out.println(d1);
The boolean data type is declared by the boolean
keyword and can only take values true
or false
:
boolean isJavaFun = true;
boolean isFishTasty = false;
System.out.println(isJavaFun); // Outputs true
System.out.println(isFishTasty); // Outputs false
The rational values are used extensively in conditional testing, which you will learn more about in the next chapter.
The char
data type is used to store a single character. The character should be surrounded by single quotes, such as 'A' or 'c':
char myGrade = 'B';
System.out.println(myGrade);
Alternatively, you can use ASCII values to display specific characters:
char myVar1 = 65, myVar2 = 66, myVar3 = 67;
System.out.println(myVar1);
System.out.println(myVar2);
System.out.println(myVar3);
String
data type is used to keep the alphabetical order (text). Character unit values should be surrounded by two quotes:
String greeting = "Hello World";
System.out.println(greeting);
String type is widely used and integrated in Java, so much so that some call it the "special ninth type".
String in Java is a type of data that is not old-fashioned, because it refers to an object. The String object has methods that are used to perform certain functions on the strings. Don't worry if you don't understand the word "object" yet. We will learn more about this unit of characters and objects in the next chapter.
Non-classical data types are called reference types
because they refer to objects.
The main differences between primitive and non-primitive types of data are:
String
).null
.