Variable Declarations and Conversions in Java

 
V
a
r
i
a
b
l
e
 
D
e
c
l
a
r
a
t
i
o
n
s
 
a
n
d
C
o
n
v
e
r
s
i
o
n
s
 
C
h
a
p
t
e
r
 
3
 
I
n
 
t
h
i
s
 
l
e
s
s
o
n
 
y
o
u
 
w
i
l
l
 
b
e
 
a
b
l
e
 
t
o
:
 
Properly declare variables using different
data types
Know the different types of variable
declarations
Differentiate variables from constants
Perform Type casting
this lesson you will be able to:
 
D
e
c
l
a
r
i
n
g
 
V
a
r
i
a
b
l
e
s
 
In Java, you must explicitly declare all
variables before using them. This rule
is in contrast to some languages
most notably Basic and earlier
versions of Visual Basic, which let
you use variables that have not been
automatically declared.
 
V
a
r
i
a
b
l
e
 
Is a name for a memory location that stores a
specific value, such as numbers and letters.
 
A variable is an identifier. It can hold only one
(1) value at a time, but its value may change
during program execution.
 
T
y
p
e
s
 
o
f
 
V
a
r
i
a
b
l
e
 
d
e
c
l
a
r
a
t
i
o
n
s
 
 
Class Variables
Instance Variables
Local Variables
Parameter Variables
 
C
l
a
s
s
 
V
a
r
i
a
b
l
e
 
A class variable is a variable that any method
in a class can access, including static
methods such as main.
 
W
h
e
n
 
d
e
c
l
a
r
i
n
g
 
a
 
c
l
a
s
s
 
v
a
r
i
a
b
l
e
,
 
y
o
u
h
a
v
e
 
t
w
o
 
b
a
s
i
c
 
r
u
l
e
s
 
t
o
 
f
o
l
l
o
w
:
 
You must place the declaration within the
body of the class but not within any of the
class methods.
 
Y
o
u
 
m
u
s
t
 
i
n
c
l
u
d
e
 
t
h
e
 
w
o
r
d
 
s
t
a
t
i
c
 
i
n
 
t
h
e
d
e
c
l
a
r
a
t
i
o
n
.
 
T
h
e
 
w
o
r
d
 
s
t
a
t
i
c
 
c
o
m
e
s
 
b
e
f
o
r
e
t
h
e
 
v
a
r
i
a
b
l
e
 
t
y
p
e
.
 
I
n
s
t
a
n
c
e
 
V
a
r
i
a
b
l
e
s
 
An instance variable is similar to a
class variable but does not specify
the word static in its declaration
 
l
o
c
a
l
 
v
a
r
i
a
b
l
e
s
 
A local variable is a variable declared within
the body of a method. Then you can use the
variable only within that method.
 
F
i
n
a
l
 
v
a
r
i
a
b
l
e
s
 
(
C
o
n
s
t
a
n
t
)
 
A final variable, also called a constant, is a
variable whose value you can’t change after
it’s been initialized.
 
T
y
p
e
 
c
a
s
t
i
n
g
 
This refers to converting a value from a
specific type to a variable of another
type
 
T
w
o
 
t
y
p
e
s
 
o
f
 
c
o
n
v
e
r
s
i
o
n
 
W
i
d
e
n
i
n
g
 
c
o
n
v
e
r
s
i
o
n
 
(
i
m
p
l
i
c
i
t
 
c
a
s
t
i
n
g
)
N
a
r
r
o
w
i
n
g
 
c
o
n
v
e
r
s
i
o
n
 
(
e
x
p
l
i
c
i
t
 
c
a
s
t
i
n
g
)
 
W
i
d
e
n
i
n
g
 
c
o
n
v
e
r
s
i
o
n
 
(
i
m
p
l
i
c
i
t
c
a
s
t
i
n
g
)
 
The conversion of the lower precision
data type to a value of a higher
precision data type. This causes no loss
of information, and the Java Virtual
Machine (JVM) will perform the casting
implicitly or automatically
 
N
a
r
r
o
w
i
n
g
 
c
o
n
v
e
r
s
i
o
n
 
(
e
x
p
l
i
c
i
t
c
a
s
t
i
n
g
)
 
The conversion of a higher precision
data type into a value of a lower
precision data type. This will typically
involve loss of information
 
W
r
a
p
p
e
r
 
C
l
a
s
s
 
Every primitive type has a
corresponding class defined in the Java
API class library.
 
W
r
a
p
p
e
r
 
C
l
a
s
s
 
This class is sometimes called a
wrapper class because it wraps a
primitive value with the object-oriented
equivalent of pretty wrapping paper and
a bow to make the primitive type look
and behave like an object.
 
 
T
r
u
e
 
o
r
 
F
a
l
s
e
 
T
h
e
 
c
a
s
t
i
n
g
 
i
n
 
N
a
r
r
o
w
i
n
g
 
c
o
n
v
e
r
s
i
o
n
 
i
s
 
n
o
t
d
o
n
e
 
b
y
 
J
V
M
 
a
n
d
 
s
h
o
u
l
d
 
b
e
 
m
a
d
e
e
x
p
l
i
c
i
t
 
b
y
 
t
h
e
 
p
r
o
g
r
a
m
m
e
r
 
t
h
r
o
u
g
h
 
a
c
a
s
t
 
o
p
e
r
a
t
o
r
.
 
T
r
u
e
 
o
r
 
F
a
l
s
e
 
A class variable is a variable that any
method in a class can access, including
static methods such as main.
 
T
r
u
e
 
o
r
 
F
a
l
s
e
 
Class variables must place within
the body of the method
 
T
r
u
e
 
o
r
 
F
a
l
s
e
 
Y
o
u
 
c
a
n
n
o
t
 
u
s
e
 
i
n
s
t
a
n
c
e
 
v
a
r
i
a
b
l
e
w
i
t
h
i
n
 
a
 
n
o
n
-
s
t
a
t
i
c
 
m
e
t
h
o
d
 
d
e
c
l
a
r
e
d
w
i
t
h
i
n
 
t
h
e
 
c
l
a
s
s
 
w
h
e
r
e
 
t
h
e
 
i
n
s
t
a
n
c
e
v
a
r
i
a
b
l
e
 
i
s
 
d
e
c
l
a
r
e
d
.
 
T
r
u
e
 
o
r
 
F
a
l
s
e
 
A wrapper class wraps a primitive value with
the object-oriented equivalent of pretty
wrapping paper and a bow to make the
primitive type look and behave like an object.
Slide Note
Embed
Share

Properly declaring variables in Java is essential before using them. This chapter covers different types of variable declarations, including class variables, instance variables, local variables, and parameter variables. It also explains the concept of type casting and the importance of explicitly declaring variables before usage in Java.

  • Java
  • Variable declarations
  • Type casting
  • Variables

Uploaded on Jul 19, 2024 | 1 Views


Download Presentation

Please find below an Image/Link to download the presentation.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author. Download presentation by click this link. If you encounter any issues during the download, it is possible that the publisher has removed the file from their server.

E N D

Presentation Transcript


  1. Chapter 3 Variable Declarations and Conversions

  2. In this lesson you will be able to: Properly declare variables using different data types Know the different types of variable declarations Differentiate variables from constants Perform Type casting this lesson you will be able to:

  3. Declaring Variables In Java, you must explicitly declare all variables before using them. This rule is in contrast to some languages most notably Basic and earlier versions of Visual Basic, which let you use variables that have not been automatically declared.

  4. Variable Is a name for a memory location that stores a specific value, such as numbers and letters. A variable is an identifier. It can hold only one (1) value at a time, but its value may change during program execution.

  5. Types of Variable declarations Class Variables Instance Variables Local Variables Parameter Variables

  6. Class Variable A class variable is a variable that any method in a class can access, including static methods such as main.

  7. When declaring a class variable, you have two basic rules to follow: You must place the declaration within the body of the class but not within any of the class methods. You must include the word static in the declaration. The word static comes before the variable type.

  8. Instance Variables An instance variable is similar to a class variable but does not specify the word static in its declaration

  9. local variables A local variable is a variable declared within the body of a method. Then you can use the variable only within that method.

  10. Final variables (Constant) A final variable, also called a constant, is a variable whose value you can t change after it s been initialized.

  11. Type casting This refers to converting a value from a specific type to a variable of another type

  12. Two types of conversion Widening conversion (implicit casting) Narrowing conversion (explicit casting)

  13. Widening conversion (implicit casting) The conversion of the lower precision data type to a value of a higher precision data type. This causes no loss of information, and the Java Virtual Machine (JVM) will perform the casting implicitly or automatically

  14. Narrowing conversion (explicit casting) The conversion of a higher precision data type into a value of a lower precision data type. This will typically involve loss of information

  15. Wrapper Class Every primitive type has a corresponding class defined in the Java API class library.

  16. Wrapper Class This class is sometimes called a wrapper class because it wraps a primitive value with the object-oriented equivalent of pretty wrapping paper and a bow to make the primitive type look and behave like an object.

  17. Activity Time

  18. True or False The casting in Narrowing conversion is not done by JVM and should be made explicit by the programmer through a cast operator.

  19. True or False A class variable is a variable that any method in a class can access, including static methods such as main.

  20. True or False Class variables must place within the body of the method

  21. True or False You cannot use instance variable within a non-static method declared within the class where the instance variable is declared.

  22. True or False A wrapper class wraps a primitive value with the object-oriented equivalent of pretty wrapping paper and a bow to make the primitive type look and behave like an object.

More Related Content

giItT1WQy@!-/#giItT1WQy@!-/#giItT1WQy@!-/#