How to Create a Variable in Java
Create a simple Java program., Scroll to a place where you want to insert the variable., Create the variable., Understand how this works., Protect variables from being edited later, optionally, by adding "final type name" between the parentheses in...
Step-by-Step Guide
-
Step 1: Create a simple Java program.
An example is provided called Hello.java : public class Hello { public static void main(String[] args) { System.out.println("Hello World!"); -
Step 2: Scroll to a place where you want to insert the variable.
Remember:
If you place a variable in the main class, you can reference it anywhere.
Choose the type of variable you need.
Integer data types :
Used to store integer values like 3, 4,
-34 etc byte short int long Floating Point data type :
Used to store numbers having fractional part like
3.479 float double Character data type:
Used to store characters like 's'
'r'
'g'
'f' etc char Boolean data types :
Can store either of the two values: true and false boolean Reference data types :
Used to store references to objects Array types Object types like String , Here are examples of how to create and assign a value to each type. int someNumber = 0; double someDouble =
635.29; float someDecimal =
4.43f; boolean trueFalse = true; String someSentence = "My dog ate a toy"; char someChar = 'f' -
Step 3: Create the variable.
It is basically "type name = value". , final int someNumber = 35; Adding the 'final' here means that the variable 'someNumber' cannot be changed later -
Step 4: Understand how this works.
-
Step 5: Protect variables from being edited later
-
Step 6: optionally
-
Step 7: by adding "final type name" between the parentheses in the second line of your code (public static void main).
Detailed Guide
An example is provided called Hello.java : public class Hello { public static void main(String[] args) { System.out.println("Hello World!");
Remember:
If you place a variable in the main class, you can reference it anywhere.
Choose the type of variable you need.
Integer data types :
Used to store integer values like 3, 4,
-34 etc byte short int long Floating Point data type :
Used to store numbers having fractional part like
3.479 float double Character data type:
Used to store characters like 's'
'r'
'g'
'f' etc char Boolean data types :
Can store either of the two values: true and false boolean Reference data types :
Used to store references to objects Array types Object types like String , Here are examples of how to create and assign a value to each type. int someNumber = 0; double someDouble =
635.29; float someDecimal =
4.43f; boolean trueFalse = true; String someSentence = "My dog ate a toy"; char someChar = 'f'
It is basically "type name = value". , final int someNumber = 35; Adding the 'final' here means that the variable 'someNumber' cannot be changed later
About the Author
Timothy Cook
Timothy Cook is an experienced writer with over 5 years of expertise in lifestyle and practical guides. Passionate about sharing practical knowledge, Timothy creates easy-to-follow guides that help readers achieve their goals.
Rate This Guide
How helpful was this guide? Click to rate: