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, as you’ve already seen:
HTML
<div class="codeblock">
<br><pre>int gear = 1;</pre><br>
</div>
- byte: The
byte
data type is an 8-bit signed two’s complement integer. It has a minimum value of -128 and a maximum value of 127 (inclusive). Thebyte
data type can be useful for saving memory in large arrays, where the memory savings actually matters. They can also be used in place ofint
where their limits help to clarify your code; the fact that a variable’s range is limited can serve as a form of documentation. - short: The
short
data type is a 16-bit signed two’s complement integer. It has a minimum value of -32,768 and a maximum value of 32,767 (inclusive). As withbyte
, the same guidelines apply: you can use ashort
to save memory in large arrays, in situations where the memory savings actually matters. - int: By default, the
int
data type is a 32-bit signed two’s complement integer, which has a minimum value of -231 and a maximum value of 231-1. In Java SE 8 and later, you can use theint
data type to represent an unsigned 32-bit integer, which has a minimum value of 0 and a maximum value of 232-1. Use the Integer class to useint
data type as an unsigned integer. See the section The Number Classes for more information. Static methods likecompareUnsigned
,divideUnsigned
etc have been added to theInteger
class to support the arithmetic operations for unsigned integers. - long: The
long
data type is a 64-bit two’s complement integer. The signed long has a minimum value of -263 and a maximum value of 263-1. In Java SE 8 and later, you can use thelong
data type to represent an unsigned 64-bit long, which has a minimum value of 0 and a maximum value of 264-1. Use this data type when you need a range of values wider than those provided byint
. TheLong
class also contains methods likecompareUnsigned
,divideUnsigned
etc to support arithmetic operations for unsigned long. - float: The
float
data type is a single-precision 32-bit IEEE 754 floating point. Its range of values is beyond the scope of this discussion, but is specified in the Floating-Point Types, Formats, and Values section of the Java Language Specification. As with the recommendations forbyte
andshort
, use afloat
(instead ofdouble
) if you need to save memory in large arrays of floating point numbers. This data type should never be used for precise values, such as currency. For that, you will need to use the java.math.BigDecimal class instead. Numbers and Strings coversBigDecimal
and other useful classes provided by the Java platform. - double: The
double
data type is a double-precision 64-bit IEEE 754 floating point. Its range of values is beyond the scope of this discussion, but is specified in the Floating-Point Types, Formats, and Values section of the Java Language Specification. For decimal values, this data type is generally the default choice. As mentioned above, this data type should never be used for precise values, such as currency. - boolean: The
boolean
data type has only two possible values:true
andfalse
. Use this data type for simple flags that track true/false conditions. This data type represents one bit of information, but its “size” isn’t something that’s precisely defined. - char: The
char
data type is a single 16-bit Unicode character. It has a minimum value of'\u0000'
(or 0) and a maximum value of'\uffff'
(or 65,535 inclusive).
Type | Description | Default | Size | Example Literals |
---|---|---|---|---|
boolean | true or false | false | 1 bit | true, false |
byte | twos complement integer | 0 | 8 bits | (none) |
char | Unicode character | \u0000 | 16 bits | 'a', '\u0041', '\101', '\\', '\'', '\n', 'ß' |
short | twos complement integer | 0 | 16 bits | (none) |
int | twos complement integer | 0 | 32 bits | -2, -1, 0, 1, 2, 1500 |
long | twos complement integer | 0 | 64 bits | -2L, -1L, 0L, 1L, 2L |
float | IEEE 754 floating point | 0.0 | 32 bits | 1.23e100f, -1.23e-100f, .3f, 3.14F |
double | IEEE 754 floating point | 0.0 | 64 bits | 1.23456e300d, -1.23456e-300d, 1e1d |
Simple Example
This example shows the basic use of different primitive data types.
Java
public class BasicDataTypesExample {
public static void main(String[] args) {
// byte - 8-bit signed integer
byte byteValue = 100;
// short - 16-bit signed integer
short shortValue = 10000;
// int - 32-bit signed integer
int intValue = 100000;
// long - 64-bit signed integer
long longValue = 10000000000L;
// float - single-precision 32-bit floating point
float floatValue = 10.5f;
// double - double-precision 64-bit floating point
double doubleValue = 100.5;
// char - single 16-bit Unicode character
char charValue = 'A';
// boolean - true or false
boolean booleanValue = true;
// Printing values
System.out.println("Byte Value: " + byteValue);
System.out.println("Short Value: " + shortValue);
System.out.println("Int Value: " + intValue);
System.out.println("Long Value: " + longValue);
System.out.println("Float Value: " + floatValue);
System.out.println("Double Value: " + doubleValue);
System.out.println("Char Value: " + charValue);
System.out.println("Boolean Value: " + booleanValue);
}
}
Output:
Java
Byte Value: 100
Short Value: 10000
Int Value: 100000
Long Value: 10000000000
Float Value: 10.5
Double Value: 100.5
Char Value: A
Boolean Value: true
Complex Example
This example demonstrates the use of both primitive and non-primitive data types in a more complex scenario involving a basic simulation of a student’s record.
Java
public class ComplexDataTypesExample {
public static void main(String[] args) {
// Primitive data types
int studentId = 12345;
char grade = 'A';
double gpa = 3.85;
boolean isGraduated = false;
// Non-primitive data types
String studentName = "John Doe";
int[] scores = {85, 90, 88, 92, 87};
Student student = new Student(studentId, studentName, grade, gpa, isGraduated, scores);
// Displaying student information
System.out.println("Student Information:");
System.out.println("ID: " + student.getId());
System.out.println("Name: " + student.getName());
System.out.println("Grade: " + student.getGrade());
System.out.println("GPA: " + student.getGpa());
System.out.println("Graduated: " + student.isGraduated());
System.out.println("Scores: " + java.util.Arrays.toString(student.getScores()));
}
}
class Student {
private int id;
private String name;
private char grade;
private double gpa;
private boolean graduated;
private int[] scores;
public Student(int id, String name, char grade, double gpa, boolean graduated, int[] scores) {
this.id = id;
this.name = name;
this.grade = grade;
this.gpa = gpa;
this.graduated = graduated;
this.scores = scores;
}
// Getter methods
public int getId() { return id; }
public String getName() { return name; }
public char getGrade() { return grade; }
public double getGpa() { return gpa; }
public boolean isGraduated() { return graduated; }
public int[] getScores() { return scores; }
}
Output:
Java
Student Information:
ID: 12345
Name: John Doe
Grade: A
GPA: 3.85
Graduated: false
Scores: [85, 90, 88, 92, 87]
These outputs provide a clear demonstration of how the different data types are used and the kind of results you can expect when running the programs.