Skip to content

Commit afab08c

Browse files
committed
Add Variables and Literals
1 parent 7b9176b commit afab08c

File tree

1 file changed

+255
-0
lines changed

1 file changed

+255
-0
lines changed
Lines changed: 255 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,255 @@
1+
---
2+
title: Java Variables and Literals
3+
description: Variables and Literals in Java
4+
---
5+
6+
import { Heading } from '@/components/Heading'
7+
import Link from 'next/link'
8+
import { TipInfo } from '@/components/Tip'
9+
10+
11+
In this tutorial, we will learn about Java variables and literals with the help of examples.
12+
13+
## Java Variables
14+
15+
A variable is a location in memory (storage area) to hold data.
16+
17+
To indicate the storage area, each variable should be given a unique name (identifier). Learn more about [Java identifiers](https://javaistic.vercel.app/java-introduction/keywords-identifiers).
18+
19+
---
20+
21+
### Create Variables in Java
22+
23+
Here's how we create a variable in Java,
24+
25+
```java
26+
int speedLimit = 80;
27+
28+
```
29+
30+
Here, `speedLimit` is a variable of `int` data type and we have assigned value **80** to it.
31+
32+
The int data type suggests that the variable can only hold integers.
33+
To learn more, visit [Java data types](/docs/variables-primitive-data-types).
34+
35+
In the example, we have assigned value to the variable during declaration. However, it's not mandatory.
36+
37+
You can declare variables and assign variables separately. For example,
38+
39+
```java
40+
int speedLimit;
41+
speedLimit = 80;
42+
```
43+
<p className="mt-4"/>
44+
45+
<TipInfo>
46+
47+
**Note :** Java is a statically-typed language. It means that all variables
48+
must be declared before they can be used.
49+
50+
</TipInfo>
51+
52+
---
53+
54+
### Change values of variables
55+
56+
The value of a variable can be changed in the program, hence the name variable. For example,
57+
58+
```java
59+
int speedLimit = 80;
60+
... .. ...
61+
speedLimit = 90;
62+
```
63+
64+
Here, initially, the value of `speedLimit` is **80**. Later, we changed it to **90**.
65+
66+
However, we cannot change the data type of a variable in Java within the same scope.
67+
68+
What is the variable scope?
69+
70+
Don't worry about it for now. Just remember that we can't do something like this:
71+
72+
```java
73+
int speedLimit = 80;
74+
... .. ...
75+
float speedLimit;
76+
```
77+
78+
To learn more, visit: [Can I change declaration type for a variable in Java](http://stackoverflow.com/questions/27092245/can-i-change-declaration-type-for-a-variable-in-java)?
79+
80+
---
81+
82+
### Rules for Naming Variables in Java
83+
84+
Java programming language has its own set of rules and conventions for naming variables. Here's what you need to know:
85+
86+
- Java is case sensitive. Hence, `age` and `AGE` are two different variables. For example,
87+
88+
```java
89+
int age = 24;
90+
int AGE = 25;
91+
92+
System.out.println(age); // prints 24
93+
System.out.println(AGE); // prints 25
94+
```
95+
96+
- Variables must start with either a **letter** or an **underscore**, **\_** or a **dollar**, **$** sign. For example,
97+
98+
```java
99+
int age; // valid name and good practice
100+
int _age; // valid but bad practice
101+
int $age; // valid but bad practice
102+
```
103+
104+
- Variable names cannot start with numbers. For example,
105+
106+
```java
107+
int 1age; // invalid variables
108+
```
109+
110+
- Variable names can't use whitespace. For example,
111+
112+
```java
113+
int my age; // invalid variables
114+
```
115+
116+
Here, is we need to use variable names having more than one word, use all lowercase letters for the first word and capitalize the first letter of each subsequent word. For example, `myAge`.
117+
118+
- When creating variables, choose a name that makes sense. For example, `score` , `number` , `level` makes more sense than variable names such as `s` ,`n` , and `l`.
119+
- If you choose one-word variable names, use all lowercase letters. For example, it's better to use `speed` rather than `SPEED`, or `sPEED`.
120+
121+
---
122+
123+
There are 4 types of variables in Java programming language:
124+
125+
- **Instance Variables (Non-Static Fields)**
126+
- **Class Variables (Static Fields)**
127+
- **Local Variables**
128+
- **Parameters**
129+
130+
If you are interested to learn more about it now, visit [Java Variable Types](https://docs.oracle.com/javase/tutorial/java/nutsandbolts/variables.html).
131+
132+
---
133+
134+
## Java Literals
135+
136+
Literals are data used for representing fixed values. They can be used directly in the code. For example,
137+
138+
```java
139+
int a = 1;
140+
float b = 2.5;
141+
char c = 'F';
142+
```
143+
144+
Here, `1` , `2.5` , and `'F'` are literals.
145+
146+
Here are different types of literals in Java.
147+
148+
---
149+
150+
### 1. Boolean Literals
151+
152+
In Java, boolean literals are used to initialize boolean data types. They can store two values: true and false. For example,
153+
154+
```java
155+
boolean flag1 = false;
156+
boolean flag2 = true;
157+
```
158+
159+
Here, `false` and `true` are two boolean literals.
160+
161+
---
162+
163+
### 2. Integer Literals
164+
165+
An integer literal is a numeric value(associated with numbers) without any fractional or exponential part. There are 4 types of integer literals in Java:
166+
167+
1. binary (base 2)
168+
2. decimal (base 10)
169+
3. octal (base 8)
170+
4. hexadecimal (base 16)
171+
172+
For example:
173+
174+
```java
175+
// binary
176+
int binaryNumber = 0b10010;
177+
// octal
178+
int octalNumber = 027;
179+
180+
// decimal
181+
int decNumber = 34;
182+
183+
// hexadecimal
184+
int hexNumber = 0x2F; // 0x represents hexadecimal
185+
// binary
186+
int binNumber = 0b10010; // 0b represents binary
187+
188+
```
189+
190+
In Java, binary starts with **0b**, octal starts with **0**, and hexadecimal starts with **0x**.
191+
192+
<p className="mt-4"/>
193+
194+
<TipInfo>
195+
196+
**Note :** Integer literals are used to initialize variables of integer types
197+
like `byte` , `short` , `int` , and `long` .
198+
</TipInfo>
199+
200+
---
201+
202+
### 3. Floating-point Literals
203+
204+
A floating-point literal is a numeric literal that has either a fractional form or an exponential form. For example,
205+
206+
```java
207+
class Main {
208+
public static void main(String[] args) {
209+
double myDouble = 3.4;
210+
float myFloat = 3.4F;
211+
212+
// 3.445*10^2
213+
double myDoubleScientific = 3.445e2;
214+
215+
System.out.println(myDouble); // prints 3.4
216+
System.out.println(myFloat); // prints 3.4
217+
System.out.println(myDoubleScientific); // prints 344.5
218+
219+
}
220+
}
221+
```
222+
<p className="mt-4"/>
223+
224+
<TipInfo>
225+
226+
**Note :** The floating-point literals are used to initialize `float` and `double`
227+
type variables.
228+
229+
</TipInfo>
230+
231+
---
232+
### 4. Character Literals
233+
234+
Character literals are unicode character enclosed inside single quotes. For example,
235+
236+
```java
237+
char letter = 'a';
238+
```
239+
240+
Here, `a` is the character literal.
241+
242+
We can also use escape sequences as character literals. For example, **\b** (backspace), **\t** (tab), **\n** (new line), etc.
243+
244+
---
245+
246+
### 5. String Literals
247+
248+
A string literal is a sequence of characters enclosed inside double-quotes. For example,
249+
250+
```java
251+
String str1 = "Java Programming";
252+
String str2 = "Javaistic";
253+
```
254+
255+
Here, `Java Programming` and `Javaistic` are two string literals.

0 commit comments

Comments
 (0)