Beginner Java Concepts

a-Rye
23.6K views

Open Source Your Knowledge, Become a Contributor

Technology knowledge has to be shared and made accessible for free. Join the movement.

Create Content
Previous: Comments Next: Objects

Primitive Variables

Primitive Variables are simple, single value data that we store for later use. In Java, you need to specify the type of variable whenever you declare a new variable. However, Java is more forgiving with print statements and variable types. The types of primitive variables are:

int myInt = 1;
float myFloat = 1.0f;
double myDouble = 5.00;
char myChar = 'A';
boolean myBoolean = true;

System.out.println("My int is: " + myInt);
System.out.println("My float is: " + myFloat);
System.out.println("My int is: " + myDouble);
System.out.println("My int is: " + myChar);
System.out.println("My int is: " + myBoolean);

Scope

Scope refers to whether a variable is able to be used in different parts of the code. This can also be thought of as relevancy. When declaring variables, you can either declare global or local variables. Declaring a variable globally means that you are able to use it throughout the rest of your class file. Local variables are declared, used, and only available in the methods which you declared them in.

Open Source Your Knowledge: become a Contributor and help others learn. Create New Content