Variables in Java
A variable is a name which is associated with a value that can be changed. For example when I write int i=10; here variable name is i which is associated with value 10, int is a data type that represents that this variable can hold integer values.
Declare Variables
To declare a variable follow this syntax:
data_type variable_name = value;
Here value is optional because in java, you can declare the variable first and then later assign the value to it. Here num is a variable and int is a data type:
int num;
Similarly we can assign the values to the variables while declaring them, like this:
char ch = 'A'; int number = 100;
or we can do it like this:
char ch; int number; ... ch = 'A'; number = 100;
Variables naming convention in Java
- Variables naming cannot contain white spaces, for example: int num ber = 100; is invalid because the variable name has space in it.
- Variable name can begin with special characters such as $ and _ but should be avoided
- As per the java coding standards the variable name should begin with a lower case letter, for example int number; For lengthy variables names that has more than one words do it like this: int smallNumber; int bigNumber; (start the second word with capital letter).
- Variable names are case sensitive in Java.
Types of variables
The Java programming language defines the following kinds of variables:
Instance Variables (Non-Static Fields): Used to store states of objects.
Class Variables (Static Fields): A class variable is any field declared with the static modifier. this tells the compiler that there is exactly one copy of this variable in existence.
Local Variables: Similar to how an object stores its state in fields, a method will often store its temporary state in local variables. As such, local variables are only visible to the methods in which they are declared. they are not accessible from the rest of the class.
Parameters: Variables which are given to methods as an input.
Data types
Data types define the values that a variable can take, for example if a variable has int data type, it can only take integer values. In java we have two categories of data type:
- Primitive data types: single values
(int, char, float etc.) - Non-primitive data types: multiple values built out of primitive data types
(Arrays, Strings)
Java is a statically typed language. A language is statically typed, if the data type of a variable is known at compile time. This means that you must specify the type of the variable (Declare the variable) before you can use it.
Primitive data types
In Java, we have eight primitive data types: boolean, char, byte, short, int, long, float and double. Java developers included these data types to maintain the portability of java as the size of these primitive data types do not change from one operating system to another:
byte, short, int and long data types are used for storing whole numbers.
float and double are used for fractional numbers.
char is used for storing characters(letters).
boolean data type is used for variables that holds either true or false.
Size in bytes and range
Although some data types hold the same type of value they can differ in their range:
Type | Size in Bytes | Range | Default value |
---|---|---|---|
byte | 1 | -128 to 127 | 0 |
short | 2 | -32,768 to 32,767 | 0 |
int | 4 | -2,147,483,648 to 2,147,483, 647 | 0 |
long | 8 | -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807 | 0 |
float | 4 | approximately ±3.40282347E+38F (6-7 significant decimal digits) | 0.0f |
double | 8 | approximately ±1.79769313486231570E+308 (15-16 significant decimal digits) | 0.0d |
char | 2 | 0 to 65,536 | '\u0000' |
boolean | - | true or false | false |
Wrapper classes
A Wrapper class is a class whose object wraps or contains a primitive data type. When we create an object to a wrapper class, it contains a field and in this field, we can store a primitive data type. The default value of a wrapper class is null. A object has to be created in order to store a value. There exists a wrapper class for each primitive data type:
Primitive data type | Wrapper class |
---|---|
byte | Byte |
short | Short |
int | Integer |
long | Long |
float | Float |
double | Double |
char | Character |
boolean | Boolean |
Literals
A literal is a fixed value that we assign to a variable in a Program. Here value 10 is a Integer literal:
int num = 10;
Integer Literal
Integer literals are assigned to the variables of data type byte, short, int and long.
byte b = 100;
short s = 200;
int num = 13313131;
long l = 928389283L;
Float Literals
Used for data type float and double. Note: Always suffix float value with the “f” else compiler will consider it as double.
double num1 = 22.4;
float num2 = 22.4f;
Char and String Literal
Used for char and String type.
char ch = 'Z';
String str = "BeginnersBook";
Source:
- https://beginnersbook.com/2017/08/variables-in-java/
- https://beginnersbook.com/2017/08/data-types-in-java/
- https://docs.oracle.com/javase/tutorial/java/nutsandbolts/variables.html
- http://cs-fundamentals.com/java-programming/java-primitive-data-types.php
- https://www.geeksforgeeks.org/wrapper-classes-java/