Menu Log in
L CSS JAVASCRIPT SQL PYTHON JAVA PH
Java Data Types
‹ Previous Next ›
Java Data Types
As explained in the previous chapter, a variable in
Java must be a specified data type:
Example Get your own Java Server
int myNum = 5; // Integer (who
float myFloatNum = 5.99f; // Floating poi
char myLetter = 'D'; // Character
boolean myBool = true; // Boolean
String myText = "Hello"; // String
Try it Yourself »
Data types are divided into two groups:
Primitive data types - includes byte , short ,
int , long , float , double , boolean and
char
Non-primitive data types - such as String ,
Arrays and Classes (you will learn more about
these in a later chapter)
Primitive Data Types
A primitive data type specifies the size and type of
variable values, and it has no additional methods.
There are eight primitive data types in Java:
Data Size Description
Type
byte 1 Stores whole numbers from
byte -128 to 127
short 2 Stores whole numbers from
bytes -32,768 to 32,767
int 4 Stores whole numbers from
bytes -2,147,483,648 to
2,147,483,647
long 8 Stores whole numbers from
bytes -9,223,372,036,854,775,808
to
9,223,372,036,854,775,807
float 4 Stores fractional numbers.
bytes Sufficient for storing 6 to 7
decimal digits
double 8 Stores fractional numbers.
bytes Sufficient for storing 15
decimal digits
boolean 1 bit Stores true or false values
char 2 Stores a single
bytes character/letter or ASCII
values
Test Yourself With Exercises
Exercise:
Add the correct data type for the following
variables:
myNum = 9;
myFloatNum = 8.99f;
myLetter = 'A';
myBool = false;
myText = "Hello World";
Submit Answer »
Start the Exercise
‹ Previous Next ›
W3schools Pathfinder
Track your progress - it's free!
Sign Up Log in
ADVERTISEMENT
COLOR PICKER
ADVERTISEMENT
PLUS
SPACES
GET CERTIFIED
FOR TEACHERS
FOR BUSINESS
CONTACT US
Top Tutorials
HTML Tutorial
CSS Tutorial
JavaScript Tutorial
How To Tutorial
SQL Tutorial
Python Tutorial
[Link] Tutorial
Bootstrap Tutorial
PHP Tutorial
Java Tutorial
C++ Tutorial
jQuery Tutorial
Top References
HTML Reference
CSS Reference
JavaScript Reference
SQL Reference
Python Reference
[Link] Reference
Bootstrap Reference
PHP Reference
HTML Colors
Java Reference
Angular Reference
jQuery Reference
Top Examples
HTML Examples
CSS Examples
JavaScript Examples
How To Examples
SQL Examples
Python Examples
[Link] Examples
Bootstrap Examples
PHP Examples
Java Examples
XML Examples
jQuery Examples
Get Certified
HTML Certificate
CSS Certificate
JavaScript Certificate
Front End Certificate
SQL Certificate
Python Certificate
PHP Certificate
jQuery Certificate
Java Certificate
C++ Certificate
C# Certificate
XML Certificate
FORUM ABOUT ACADEMY
W3Schools is optimized for learning and
training. Examples might be simplified to
improve reading and learning. Tutorials,
references, and examples are constantly
reviewed to avoid errors, but we cannot
warrant full correctness of all content. While
using W3Schools, you agree to have read
and accepted our terms of use, cookie and
privacy policy.
Copyright 1999-2024 by Refsnes Data. All
Rights Reserved. W3Schools is Powered by
[Link].