Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

Java Variable Types: A Beginner's Guide, Exercises of Java Programming

fgr rkogtjr ggrret re trertr tertr

Typology: Exercises

2020/2021

Uploaded on 01/20/2021

shubha-agarwal-1
shubha-agarwal-1 🇮🇳

5 documents

1 / 6

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Java - Variable Types
Java - Variable Types
A variable provides us with named storage that our programs can manipulate. Each variable in Java has a specific type, which determines the size and layout of
A variable provides us with named storage that our programs can manipulate. Each variable in Java has a specific type, which determines the size and layout of
the variable's memory; the range of values that can be stored within that memory; and the set of operations that can be applied to the variable.
the variable's memory; the range of values that can be stored within that memory; and the set of operations that can be applied to the variable.
You must declare all variables before they can be used. Following is the basic form of a variable declaration
You must declare all variables before they can be used. Following is the basic form of a variable declaration −
data type variable [ = value][, variable [ = value] ...] ;
data type variable [ = value][, variable [ = value] ...] ;
Here
Here data type
data type is one of Java's datatypes and
is one of Java's datatypes and variable
variable is the name of the variable. To declare more than one variable of the specified type, you can use a comma-
is the name of the variable. To declare more than one variable of the specified type, you can use a comma-
separated list.
separated list.
Following are valid examples of variable declaration and initialization in Java
Following are valid examples of variable declaration and initialization in Java −
Example
Example
int
int a
a,
, b
b,
, c
c;
;
// Declares three ints, a, b, and c.
// Declares three ints, a, b, and c.
int
int a
a =
=
10
10,
, b
b =
=
10
10;
;
// Example of initialization
// Example of initialization
byte
byte B
B =
=
22
22;
;
// initializes a byte type variable B.
// initializes a byte type variable B.
double
double pi
pi =
=
3.14159
3.14159;
;
// declares and assigns a value of PI.
// declares and assigns a value of PI.
char
char a
a =
=
'a'
'a';
;
// the char variable a iis initialized with value 'a'
// the char variable a iis initialized with value 'a'
This chapter will explain various variable types available in Java Language. There are three kinds of variables in Java
This chapter will explain various variable types available in Java Language. There are three kinds of variables in Java −
Local variables
Local variables
Instance variables
Instance variables
Class/Static variables
Class/Static variables
Local Variables
Local Variables
Local variables are declared in methods, constructors, or blocks.
Local variables are declared in methods, constructors, or blocks.
Local variables are created when the method, constructor or block is entered and the variable will be destroyed once it exits the method, constructor, or
Local variables are created when the method, constructor or block is entered and the variable will be destroyed once it exits the method, constructor, or
block.
block.
pf3
pf4
pf5

Partial preview of the text

Download Java Variable Types: A Beginner's Guide and more Exercises Java Programming in PDF only on Docsity!

Java - Variable TypesJava - Variable Types

A variable provides us with named storage that our programs can manipulate. Each variable in Java has a specific type, which determines the size and layout of

A variable provides us with named storage that our programs can manipulate. Each variable in Java has a specific type, which determines the size and layout of

the variable's memory; the range of values that can be stored within that memory; and the set of operations that can be applied to the variable.

the variable's memory; the range of values that can be stored within that memory; and the set of operations that can be applied to the variable.

You must declare all variables before they can be used. Following is the basic form of a variable declaration −

You must declare all variables before they can be used. Following is the basic form of a variable declaration −

data type variable [ = value][, variable [ = value] ...] ;

data type variable [ = value][, variable [ = value] ...] ;

Here

Here

data type

data type

is one of Java's datatypes and

is one of Java's datatypes and

variable

variable

is the name of the variable. To declare more than one variable of the specified type, you can use a comma-

is the name of the variable. To declare more than one variable of the specified type, you can use a comma-

separated list.

separated list.

Following are valid examples of variable declaration and initialization in Java −

Following are valid examples of variable declaration and initialization in Java −

ExampleExample

int

int a

a ,

b

b ,

c

c ;

// Declares three ints, a, b, and c.

// Declares three ints, a, b, and c.

int

int a

a

b

b

// Example of initialization

// Example of initialization

byte

byte B

B

// initializes a byte type variable B.

// initializes a byte type variable B.

double double pipi == 3.141593.14159;; // declares and assigns a value of PI.// declares and assigns a value of PI.

char

char a

a

'a'

'a' ;

// the char variable a iis initialized with value 'a'

// the char variable a iis initialized with value 'a'

This chapter will explain various variable types available in Java Language. There are three kinds of variables in Java −This chapter will explain various variable types available in Java Language. There are three kinds of variables in Java −

Local variables

Local variables

Instance variables

Instance variables

Class/Static variables

Class/Static variables

Local Variables

Local Variables

Local variables are declared in methods, constructors, or blocks.

Local variables are declared in methods, constructors, or blocks.

Local variables are created when the method, constructor or block is entered and the variable will be destroyed once it exits the method, constructor, or

Local variables are created when the method, constructor or block is entered and the variable will be destroyed once it exits the method, constructor, or

block.

block.

Access modifiers cannot be used for local variables.

Access modifiers cannot be used for local variables.

Local variables are visible only within the declared method, constructor, or block. Local variables are visible only within the declared method, constructor, or block.

Local variables are implemented at stack level internally. Local variables are implemented at stack level internally.

There is no default value for local variables, so local variables should be declared and an initial value should be assigned before the first use.

There is no default value for local variables, so local variables should be declared and an initial value should be assigned before the first use.

ExampleExample

Here,Here, ageage is a local variable. This is defined insideis a local variable. This is defined inside pupAge()pupAge() method and its scope is limited to only this method.method and its scope is limited to only this method.

public public classclass TestTest {{

public

public void

void pupAge

pupAge ()

int

int age

age

age

age

age

age

System

System .

out

out .

println

println (

"Puppy age is : "

"Puppy age is : "

age

age );

public

public static

static void

void main

main (

String

String args

args [])

[])

Test

Test test

test

new

new Test

Test ();

test

test .

pupAge

pupAge ();

This will produce the following result −This will produce the following result −

Output

Output

Puppy age is: 7 Puppy age is: 7

Example

Example

Following example uses

Following example uses

age

age

without initializing it, so it would give an error at the time of compilation.

without initializing it, so it would give an error at the time of compilation.

public

public class

class Test

Test {

public public voidvoid pupAgepupAge()() {{

Live Demo

Live Demo

Live Demo

Live Demo

Instance variables can be accessed directly by calling the variable name inside the class. However, within static methods (when instance variables are

Instance variables can be accessed directly by calling the variable name inside the class. However, within static methods (when instance variables are

given accessibility), they should be called using the fully qualified name.

given accessibility), they should be called using the fully qualified name.

ObjectReference.VariableName

ObjectReference.VariableName

Example

Example

import

import java

java .

io

io .*;

public

public class

class Employee

Employee {

// this instance variable is visible for any child class.

// this instance variable is visible for any child class.

public

public String

String name

name ;

// salary variable is visible in Employee class only.

// salary variable is visible in Employee class only.

private private doubledouble salarysalary;;

// The name variable is assigned in the constructor.

// The name variable is assigned in the constructor.

public

public Employee

Employee (

String

String empName

empName )

name

name

empName

empName ;

// The salary variable is assigned a value.

// The salary variable is assigned a value.

public public voidvoid setSalarysetSalary((doubledouble empSalempSal)) {{

salary

salary

empSal

empSal ;

// This method prints the employee details.

// This method prints the employee details.

public public voidvoid printEmpprintEmp()() {{

System

System .

out

out .

println

println (

"name : "

"name : "

name

name );

System System..outout..printlnprintln(("salary :""salary :" ++ salarysalary););

public public staticstatic voidvoid mainmain((StringString argsargs[])[]) {{

Employee

Employee empOne

empOne

new

new Employee

Employee (

"Ransika"

"Ransika" );

empOne empOne..setSalarysetSalary(( 10001000 ););

empOne

empOne .

printEmp

printEmp ();

This will produce the following result −This will produce the following result −

Live Demo

Live Demo

Output

Output

name name : Ransika: Ransika

salary :1000.

salary :1000.

Class/Static Variables

Class/Static Variables

Class variables also known as static variables are declared with the static keyword in a class, but outside a method, constructor or a block.

Class variables also known as static variables are declared with the static keyword in a class, but outside a method, constructor or a block.

There would only be one copy of each class variable per class, regardless of how many objects are created from it.

There would only be one copy of each class variable per class, regardless of how many objects are created from it.

Static variables are rarely used other than being declared as constants. Constants are variables that are declared as public/private, final, and static. Static variables are rarely used other than being declared as constants. Constants are variables that are declared as public/private, final, and static.

Constant variables never change from their initial value.

Constant variables never change from their initial value.

Static variables are stored in the static memory. It is rare to use static variables other than declared final and used as either public or private constants. Static variables are stored in the static memory. It is rare to use static variables other than declared final and used as either public or private constants.

Static variables are created when the program starts and destroyed when the program stops. Static variables are created when the program starts and destroyed when the program stops.

Visibility is similar to instance variables. However, most static variables are declared public since they must be available for users of the class.

Visibility is similar to instance variables. However, most static variables are declared public since they must be available for users of the class.

Default values are same as instance variables. For numbers, the default value is 0; for Booleans, it is false; and for object references, it is null. Values

Default values are same as instance variables. For numbers, the default value is 0; for Booleans, it is false; and for object references, it is null. Values

can be assigned during the declaration or within the constructor. Additionally, values can be assigned in special static initializer blocks.

can be assigned during the declaration or within the constructor. Additionally, values can be assigned in special static initializer blocks.

Static variables can be accessed by calling with the class name

Static variables can be accessed by calling with the class name

ClassName.VariableName

ClassName.VariableName

When declaring class variables as public static final, then variable names (constants) are all in upper case. If the static variables are not public and final,

When declaring class variables as public static final, then variable names (constants) are all in upper case. If the static variables are not public and final,

the naming syntax is the same as instance and local variables.

the naming syntax is the same as instance and local variables.

ExampleExample

import import javajava..ioio.;.;

public

public class

class Employee

Employee {

// salary variable is a private static variable

// salary variable is a private static variable

private

private static

static double

double salary

salary ;

// DEPARTMENT is a constant

// DEPARTMENT is a constant

public

public static

static final

final String

String DEPARTMENT

DEPARTMENT

"Development "

"Development " ;

public

public static

static void

void main

main (

String

String args

args [])

[])

Live Demo

Live Demo