Inside of a method, like our main method, we might want to create some variables for use. Java gives us numerous primitive data types that we can use, and within a method, we declare and use them, just like we would in C/C++.
// declaring our variables
int x;
float y;
double z;
Comments
You might have noticed the comment above. Comments in Java are the same as C++.
The double forward slash (//
)is for single line comments, and nothing after the slash will be considered by the Java Compiler.
The /* ... */
is for multi, or partial, line comments. Only what is between the /*
and the */
will be ignored.
Identifies Names within Java
Identifier names within Java follow the same basic rules as with most modern languages. It must:
- start with a letter,
- it can only contain letters, numbers, and underscores, and
- it cannot be a reserved/keyword within Java
Java standard naming convention uses the camel case, sometimes called hump back, notation, where the first letter is lowercase for variables, objects, and methods. Any additional words are uppercase to make it easier to read, since spaces are not allowed in an identifier.
Differences with C++
One of the key differences with C++ is that Java does not allow you to use unsigned datatypes. So where in C/C++ you might find unsigned int x
; you cannot do that in Java. Instead, use a larger datatype, like a long
and then test to make sure it is not getting a negative value.
Now, starting in Java SE 8, you can mimic the use of an unsigned value, however, it requires using the signed value and then using the datatype class methods, which can be a bit confusing, so most people treat Java as having no unsigned variables.
Data Types
Java has numerous built in primitive data types.
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 |
Setting Primitive Literal Values
You can create a variable and assign it a value at the same time in Java, you just need to assign that value a primitive value.
Some are fairly straight forward, others as you will see from the example below, need a letter at the end of the value.
// integer style variables
byte b = 10;
short s = 256;
int i = 1200;
long l = 200L; // notice the L at the end of the value
// floating point numbers now
float f = 3.14f; // notice the f at the end of the value
double d = 3.14d; // notice the d is needed for a double
// booleans
boolean isTrue = true; // true is a built in keyword in Java as is false
// char and string
char ch = 'W'; // notice the single quotes for a character, just like C++
string str = "Hello"; // notice the double quotes for a string, just like C++
Java Variables was originally found on Access 2 Learn
2 Comments
Comments are closed.