Written by students who passed Immediately available after payment Read online or as PDF Wrong document? Swap it for free 4.6 TrustPilot
logo-home
Class notes

variables, keywords and operators in java

Rating
-
Sold
-
Pages
6
Uploaded on
09-10-2022
Written in
2022/2023

summary of variables, keywords and operators in java. point to point notes which can be understood by anybody and is beginner friendly

Institution
Course

Content preview

VARIABLES
In Java, every variable has a type. You declare a variable by placing the type first, followed by the name
of the variable. Here are some examples:

double salary; //declaring a single variable salary of type double

int vacationDays;

long earthPopulation;

boolean done;

int i,j; //declaring multiple variables

Rules for Naming Variable

A variable name must begin with a letter and must be a sequence of letters or

Digits

Java is case sensitive. Hence variable Total and total are different variables.

You also cannot use a Java reserved word(keyword) as a variable name.

You cannot have a white space for variable name.



Initializing Variables
After you declare a variable, you must explicitly initialize it by means of an

assignment statement

For Example: If variable is not initialized you get the error as below

int vacationDays;

System.out.println(vacationDays); // ERROR--variable not initialized

Example :

int vacationDays; //declaring a variable called vacationDays

VacationDays = 12; // Initializing vacationDays to 12

You can both declare and initialize a variable on the same line. For example:

int vacationDays = 12;

In Java you can put declarations anywhere in your code.

, Types of Variables
There are three types of variables in Java

1. local variable

2. instance variable

3. static variable

1) Local Variable : A variable declared inside the body of the method is called local variable. You can use
this variable only within that method and the other methods in the class aren't even aware that the
variable exists.

A local variable cannot be defined with "static" keyword.

2) Instance Variable

A variable declared inside the class but outside the body of the method, is called an instance variable. It
is not declared as static

It is called an instance variable because its value is instance-specific.

3) Static variable

A variable that is declared as static is called a static variable. It cannot be local. You can create a single
copy of the static variable and share it among all the instances of the class. Memory allocation for static
variables happens only once when the class is loaded in the memory.




Constants
In Java, you use the keyword final to denote a constant. The final modifier means that the variable's
value cannot change. Once the value is assigned, it cannot be reassigned.

class DemoProgram

{

public static void main(String args[])

{

final double PI=3.14; //PI is real constant

int radius =10;

double area;

area= PI*radius*radius;

Written for

Institution
Course

Document information

Uploaded on
October 9, 2022
Number of pages
6
Written in
2022/2023
Type
Class notes
Professor(s)
Java
Contains
All classes

Subjects

$8.39
Get access to the full document:

Wrong document? Swap it for free Within 14 days of purchase and before downloading, you can choose a different document. You can simply spend the amount again.
Written by students who passed
Immediately available after payment
Read online or as PDF

Get to know the seller
Seller avatar
niranjanik

Get to know the seller

Seller avatar
niranjanik Presidency university
Follow You need to be logged in order to follow users or courses
Sold
-
Member since
3 year
Number of followers
0
Documents
2
Last sold
-

0.0

0 reviews

5
0
4
0
3
0
2
0
1
0

Recently viewed by you

Why students choose Stuvia

Created by fellow students, verified by reviews

Quality you can trust: written by students who passed their tests and reviewed by others who've used these notes.

Didn't get what you expected? Choose another document

No worries! You can instantly pick a different document that better fits what you're looking for.

Pay as you like, start learning right away

No subscription, no commitments. Pay the way you're used to via credit card and download your PDF document instantly.

Student with book image

“Bought, downloaded, and aced it. It really can be that simple.”

Alisha Student

Working on your references?

Create accurate citations in APA, MLA and Harvard with our free citation generator.

Working on your references?

Frequently asked questions