Lecture 7 - Get Started With Java

You might also like

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 7

LECTURE 7 – GET STARTED

WITH JAVA
OUTLINE

• Variables
• Example
• Constants
• Escape Sequences
VARIABLES

• Variable is a name of memory location. There are three types of variables in java: local, instance
and static.
• Types of Variable: There are three types of variables in java:
• local variable: A variable which is declared inside the method is called local variable.
• instance variable: Instance variables in Java are non-static variables which are defined in a class
outside any method, constructor or a block. Each instantiated object of the class has a separate copy
or instance of that variable. An instance variable belongs to a class.
• static variable: A static variable is common to all the instances (or objects) of the class because it is a
class level variable. In other words you can say that only a single copy of static variable is created and
shared among all the instances of the class.
• We will have detailed learning of these variables in next lectures.
VARIABLES - EXAMPLE

• Example to understand the types of variables in java


class A
{
int data=50;//instance variable
static int m=100;//static variable
void method()
{
int n=90;//local variable
}
}//end of class
CONSTANTS

• A constant is a variable whose value cannot change once it has been assigned. Java
doesn't have built-in support for constants.
• modifier final dataType variableName = value; //global constant
• For a simple floating point value (a number with a fractional part) like Pi, it would
look something like:
• private static final double PI = 3.1415926;
• The keyword 'private' indicates visibility, the keyword 'static' indicates the storage
class, and the keyword 'double' indicates the variable type. Ignore them for now,
they are beyond the scope of this lesson. The really important part is the keyword
'final'. This is what determines that the variable 'PI' is a constant. It says that for the
life of this variable, the value will not change.
ESCAPE SEQUENCES

• A character preceded by a backslash (\) is an escape sequence and has a special


meaning to the compiler. The following is a table of escape sequences to be used
when printing in Java.

Sequence Name Meaning


\n New line Moves to beginning of next line
\b Backspace Backs up one character
Moves to next tab position
\t Horizontal tab Tab spacing is every 8 columns starting with 1.
(Columns 9, 17, 25, 33, 41, 49, 57, 65, 73 ...)
\\ Backslash Displays an actual backslash
\' Single quote Displays an actual single quote
\" Double quote Displays an actual double quote
REFERENCES
• https://www.w3schools.com/java/java_ref_keywords.asp
• https://study.com/academy/lesson/java-constants-definition-declaration-types.html
• https://www.tutorialspoint.com/escape-sequences-in-java
• http://tutorials.jenkov.com/java/ternary-operator.html
• https://www.programiz.com/java-programming/operators
• https://www.tutorialspoint.com/java/java_basic_operators.htm
• https://www.w3schools.com/java/java_operators.asp
• https://androidkennel.org/zero-day-java-guide-2-operators-and-control-flow/
• https://stackoverflow.com/questions/19151388/use-of-operator-in-java?lq=1
• https://docs.oracle.com/javase/tutorial/java/nutsandbolts/op1.html#:~:text=The%20unary%20operators%20req
uire%20only,are%20positive%20without%20this%2C%20however
)

You might also like