Java VariablesS2C Home « Java Variables

So what is a variable? Well a variable allows us to store some information that we may want to manipulate or use. The variable is like a container where we put our information for use and retrieval.

Java comes with two types of variables to choose from, primitives and reference. In this lesson we look at variable syntax, an overview of the primitive variable data types available in Java, naming rules, keywords and Java literals.

We will go into much more detail about individual primitive types in the Primitives - boolean & char data types and Primitives - Numeric data types lessons.

We discuss Reference Variables in the Objects & Classes section.

The primitive specified determines which operations are allowed on it and thus operations valid for one primitive type may be invalid for another.

Variable Syntax Top

Java is a strongly typed language and as such cares about the type of the variables we declare. So what does this statement actually mean? Well whenever you declare a variable in Java, you have to give it a type, or the compiler complains when you come to compile your code. You also have to name the variable and there are rules on naming.

type diagram
Figure 1. Java type.

Primitive Types Top

We will go into much more detail about individual primitive types in the Primitives - boolean & char data types and Primitives - Numeric data types lessons, for now here's a table of the various primitive types and information regarding each:

Primitive Type Information
Primitive Type Meaning Bit Width Range
boolean and char
booleantrue/false valuesJVM specifictrue or false
charCharacter160  to  65535 - ('\u0000' to '\uffff' Unicode)
signed numeric integers
byte8-bit integer8-128  to  127
shortShort integer16-32,768  to  32,767
intInteger32-2,147,483,648  to  2,147,483,647
longLong integer64-9,233,372,036,854,775,808  to  9,233,372,036,854,775,807
signed floating point
floatSingle-precision float32≈ ±3.40282347E+38F (6-7 significant decimal digits)
doubleDouble-precision float64≈ ±1.79769313486231570E+308 (15 significant decimal digits)

Naming Rules and Keywords Top

These rules apply to all variables, as well as classes and methods (collectively known as identifiers)

  • Must start with a letter, the underscore symbol (_) or the dollar sign ($). A name can't start with a number or any other symbol.
  • You can use numbers after the first letter, you just can't start with it.
  • You cannot use keywords or names recognized by the compiler. See the table below for the words you can't use.
Click a link in the table below to show lesson usage for any keyword you're interested in:
java Keywords
abstractassertbooleanbreakbyte
casecatchcharclassconst *
continuedefaultdodoubleelse
enumextendsfinalfinallyfloat
forgoto *ifimplementsimport
instanceofintinterfacelongnative
newpackageprivateprotectedpublic
returnshortstaticstrictfpsuper
switchsynchronizedthisthrowthrows
transienttryvoidvolatilewhile

Keywords marked with an asterisk (*) are not used.

Although not a rule as such it's also important to know that identifiers are case-sensitive.

Naming Rules Test

Try this to test your knowledge of naming rules. We will go into the types as we go through the lesson but you should be able to pick out the invalidly named identifiers from the rules above.

Statement Valid?
boolean a = true; yes noThis passes all the rules so is a valid identifier
char 1char = 'O'; yes noStarts with a numeric so invalid
char enum = 'a'; yes noUses a reserved word so invalid
int $num = true; yes noThis passes all the rules so is a valid identifier
long a123 = 234; yes noThis passes all the rules so is a valid identifier
short this = 2; yes noUses a reserved word so invalid
byte _aByte2 = 'a'; yes noThis passes all the rules so is a valid identifier
boolean &b = 'a'; yes noDoesnt start with letter, $ or _ so invalid

Java Literals Top

Java literals are values that are represented in their recognizable everyday form and we have been using them within our coding throughout the lessons so far. The table below gives examples of literals for each primitive type used in this lesson and introduces a few we haven't come across yet.

Types
Type Examples
boolean
booleantrue or false
char
char16, 'G', 'a'    (numeric or a single character enclosed in single quotes)
signed integers
byteNegative or positive integers in the range -128  to  127
shortNegative or positive integers in the range -32,768  to  32,767
intNegative or positive integers in the range -2,147,483,648  to  2,147,483,647
longNegative or positive integers in the range -9,233,372,036,854,775,808
to  9,233,372,036,854,775,807
Append a l or L to the number or it will be treated as type int.
12l, -123456L
signed floating point
floatAppend a f or F to the number or it will be treated as type double.
25.23f, -1234.56F
double12.34, -56.72456
hexadecimal constants
byte, short, int, longCan be used with any integer type as long as it is in range for that type.
0xF, 0xFA
octal constants
byte, short, int, longCan be used with any integer type as long as it is in range for that type.
05, 012
string literals
"hello", "welcome back"    (enclosed in double quotes)

The following are escape character constants that are required for rendering or printing and use the backslash symbol \ to signify the escape sequence.

\'             Escape a single quote
\"             Escape a double quote
\\             Escape a backslash
\b             Escape a backspace
\f             Escape a form feed
\n             Escape a new line
\r             Escape a carriage return
\t             Escape a tab
\ooo         Escape an octal constant where ooo is the octal constant
\uhhhh     Escape a hexidecimal constant where hhhh is the hexidecimal constant

Related Quiz

Fundamentals Quiz 2 - Java Variables Quiz

Lesson 3 Complete

In this lesson we looked at variable syntax, an overview of the primitive variable data types available in Java, naming rules, keywords and Java literals.

What's Next?

In the next lesson we take a much closer look at the boolean and char primitive variable data types.