In this tutorial, we will learn about variables in java and literals in java.
A variable is a type of named storage that our programs can access. The specific type of each variable in Java determines the size and layout of the memory of the variable, the range of values that can be stored in that memory, and the set of operations that can be applied.
- Variables are data storage containers.
- The term “variable” refers to the name of a memory location.
- Each variable should have a unique name to indicate the storage area (identifier).
Example:
int n = 5; float number = 5.99f; char charVariable = 'D'; boolean booleanVariable = true; String stringVariable = "Hello";
There are three types of variables in Java:
Local variable | Instance variable | Static variable |
---|---|---|
1: A local variable is a variable declared within the method body. This variable can only be used within that method, and the other methods in the class are unaware that it exists. 2: The keyword “static” cannot be used to define a local variable. 3: In methods, constructors, and blocks, local variables are declared. 4: For local variables, access modifiers aren’t allowed. 5: Only the declared method, constructor or block can see local variables. | 1: An instance variable is a variable declared inside the class but outside the method body. It hasn’t been declared static. Because its value is instance-specific and not shared across instances, it’s called an instance variable. 2:A slot for each instance variable value is created when space in the heap is allocated for an object. 3: When an object is created with the keyword ‘new,’ instance variables are created, and they are destroyed when the object is destroyed. 4: Values that must be referenced by more than one method, constructor, or block, or essential parts of an object’s state that must be present throughout the class, are stored in instance variables. 5: Before or after use, instance variables can be declared at the class level. Instance variables can be given access modifiers. 6: Default values exist for instance variables. The default value is 0 for numbers, false for Booleans, and null for object references. Values can be set in the constructor or during the declaration. | 1: A static variable is one that has been declared as static. It’s not possible for it to be local. You can make a single copy of the static variable and share it across all of the class’s instances. Static variables are only allocated memory once when the class is loaded into memory. 2: Class variables, also known as static variables, are declared in a class but outside of a method, constructor, or block using the static keyword. 3: Regardless of how many objects are created from it, each class variable would only have one copy. 4: Static variables are created when the program begins and destroyed when it ends. 5: The concept of visibility is similar to that of instance variables. Most static variables are declared public because they must be accessible to class users. 6: The default values are the same as the instance variables. The default value for numbers is 0, for Booleans, it is false, and for object references, it is null. Values can be assigned either during the declaration or during the constructor. Values can also be assigned in special static initializer blocks. 7: Static variables can be accessed by using the class name ClassName. VariableName. |
Contents
Local Variable Example
public class Demo { public void Sample() { int value = 10; value += 5; System.out.println("Value is : " + value); } } class Main{ public static void main(String args[]) { Demo demo = new Demo(); demo.Sample(); } }
Output:
Value is: 15 |
Instance Variable Example
public class Student { public String name; private int marks; public Student (String studentName) { name = studentName; } public void setMarks(int studentMarks) { marks = studentMarks; } public void printStudentInf() { System.out.println("Name: " + name ); System.out.println("Marks:" + marks); } public static void main(String[] args) { Student StudentOne = new Student("Sagar"); Student StudentTwo = new Student("Avdhesh"); Student StudentThree = new Student("Suraj"); StudentOne.setMarks(70); StudentTwo.setMarks(80); StudentThree.setMarks(90); StudentOne.printStudentInf(); StudentTwo.printStudentInf(); StudentThree.printStudentInf(); } }
Output:
Name: Sagar Marks:70 Name: Avdhesh Marks:80 Name: Suraj Marks:90 |
Static Variable Example
public class Student { private static int Stipend; public static final String Company = "Java Developer "; public static void main(String args[]) { Stipend = 5000; System.out.println(Company + "stipend:" + Stipend); } }
Output:
Java Developer stipend: 5000 |
Java Literals
Literals are data that are used to represent fixed values. They can be directly used in the code. A literal can be a number, a character, or a string, character.
In the expression,n = 10, for example. 10 is literal, and x is a variable. |
int x = 10; float y = 5.5; char z = 'S';
Here, 10, 5.5, and ‘S’ are literals.
1: Integer Literals
An integer literal is a numeric value (associated with numbers) that does not contain any fractional or exponential components.
In Java, there are four types of integer literals.
binary (base 2) |
decimal (base 10) |
octal (base 8) |
hexadecimal (base 16) |
Binary literals in Java begin with 0b, octal literals with 0, and hexadecimal literals with 0x.
Example:
// binary literals int binaryNumber = 0b100000; // ob represents binary literals // octal literals int octalNumber = 025; // decimal literals int decNumber = 25; // hexadecimal literals int hexNumber = 0x4F; // 0x represents hexadecimal literals |
2: Floating-point Literals
An integer part, a decimal point, a fractional part, and an exponent part comprise a floating-point literal. Floating point literals can be represented in either decimal or exponential form.
- A floating-point literal is a numeric literal with a fractional or exponential form.
Example:
9.74848 546783E-7F |
class Sample { static void Main(string[] args) { // float float x = 3.89f; Console.WriteLine(x); // float float y = 3.14564f; Console.WriteLine(y); } }
3: Character Literals
Character literals are character expressions with constant values that are embedded in a Java program. Java characters are 16-bit Unicode characters with values ranging from 0 to 65535. In Java, character literals are represented by a single quote, the character, and a closing single quote ( ‘a’, ‘7’).
instance variable in java, static variable in java, types of variables in java, local variable in java, class variable in java, instance variable in java, static variable in java, types of variables in java, local variable in java, class variable in java , instance variable in java, static variable in java, types of variables in java, local variable in java, class variable in java , instance variable in java, static variable in java, types of variables in java, local variable in java, class variable in java , instance variable in java, static variable in java, types of variables in java, local variable in java, class variable in java , instance variable in java, static variable in java, types of variables in java, local variable in java, class variable in java , instance variable in java, static variable in java, types of variables in java, local variable in java, class variable in java , instance variable in java, static variable in java, types of variables in java, local variable in java, class variable in java , instance variable in java, static variable in java, types of variables in java, local variable in java, class variable in java
- Escape sequences can also be used as a character literal. For example, \b (for backspace), \t (for tab), \n ( for new line), and so on.
4: String literals
In Java, a string literal is a sequence of characters from the source character set that Java programmers use to populate string objects or display text to the user. These characters, which are enclosed within two quotation marks (” “), could be letters, numbers, or symbols of any kind.
String x = "DevelopersDome"; String y = "Java Android development";
5: Boolean Literals
variables in java,variables in java,variables in java,variables in java,variables in java,variables in java,variables in java,variables in java
True or false are the only two values that Boolean literals can represent. In Java, a value of 1 is assumed to be true, while a value of 0 is assumed to be false.
boolean x = false; boolean y = true; |
You Might Also Like:
Features of Java | Java Tutorial
Java Basic Syntax | Java Tutorial
Difference between JDK, JRE, JVM | Java Tutorial
- We hope that this article will assist you in understanding all about Java Variables, and Java Literals We have concentrated on making a basic, meaningful, and easy-to-learn guide to the concepts. Still, if you have any problems regarding this, please post them in the comment section, and we will be glad to assist you.
java variables,java variables,java variables,java variables,java variables
Pingback: Java Data Types | Primitive and Non-Primitive Data Types