Variables in Java - tutorial
VARIABLES IN JAVA
Table of Contents
Variable
Comments
Passing arguments to a program
Rules for variable naming
Casting
Escape sequence characters
Literals
Scope of the variable
What is a variable?
A variable is a small memory location, where we can store a small amount of data. It can be any data type. Datatype can be alphabetical, numerical, alphanumerical…etc.
ex:
int a;
int → is an integer datatype, it can store only numbers.
a → is a variable name
ex:
int a,b,c; // variables declration
c = 0; // initialization
a = 5;
b = 10;
To display the value of the variable use the following statement.
System.out.println(a); // 5
System.out.println("a=" + a); // o/p is a=5
Comments:
we write our own comments in program. They are 2 types.
1.single line---> with //
2.multi line ----> multiline with
/* .....
..... */
When we comment a line, the compiler ignores it. It doesn’t print on the screen.
Datatype and their sizes
There are 8 datatype in java.
char ---- 'a';
byte ---- 1 Byte store 0's and 1's.
short ---- 2 bytes
int ---- 4 bytes
long ---- 8 bytes
float ---- 4.16(integral part is 4, decimal values are 16)
double ---- 8.32
boolean ---- false
example:
class Calculator{
public static void main(String args[])
{
int a = 10; //declaration + assigning value to a variable
int b = 20;
int c = 0;
c = a + b; // expression
System.out.println("c=" + c); // print statement
}
}
Passing arguments to a progra in Java
example:
public class ArgsDemo{ //class begin
public static void main(String args[]){ //main begin
String name = args[0];
String address = args[1];
System.out.println("Name: " + name);
System.out.println("Address: " + address);
} // main end
} // class end
example2:
public class Argsdemo2
{
public static void main(String args[])
{
int n;
n = Integer.parseInt(args[0]);
System.out.println("n * n = " + a*a);
}
}
Rules for variable naming
Valid
1) variable name starts with letter
ex: empName, city, email
2) only two special characters are allowed ($, _)
ex: $street_no, emp_no, emp_salary, customer_name
3) can begin with $(dollar sign)
ex: $street It can have '_', or in between the name ex: $street_no
Invalid
1) should not start with number. ex:123city;
2) should not allow spaces, ex: emp name
Casting
Changing value from one datatype to another.
ex: float a = 56.4;
int b = (int)a;
o/p is only int value, i.e. 56.
Write a program that takes 3 subjects marks and calculates total and prints all the details.
total = sub1 + sub2 + sub3;
avg = total/3;
print all the details.
class Marks
{
public static void main(String args[])
{
int math, sci, eng, total = 0;
float avg = 0.0f;
math = 35;
sci = 26;
eng = 34;
total = math + sci + eng;
// math+sci+eng/3 ;---wrong
avg = (math + sci + eng)/3; // correct
System.out.println("Total = " + total);
System.out.println("Average = " + avg);
}
}
Escape sequence characters
\n ---> prints in newline
\t ---> prints one tab space
Literals
In Java, literals are the constant values that can be visible directly in the program. It can be directly assigned to a variable. Java has different types of literals.
Literals Example
public class LiteralsDemo { public static void main(String args[]) { int num = 567; float floatVal = 5534.99f; double price = 18765.567; int hexaVal = 0x7e4; int binaryVal = 0b11010; int octalNum = 067; boolean boolVal = true; char ch = 'J'; char ch1 = '\u0021'; char ch2 = 1456; String str = "Java"; String stuName = null;
System.out.println(num); System.out.println(floatVal); System.out.println(price); System.out.println(hexaVal); System.out.println(binaryVal); System.out.println(ch); System.out.println(str); System.out.println(boolVal); System.out.println(octalNum); System.out.println(stuName); System.out.println(ch1); System.out.println("\t" + "backslash literal"); System.out.println(ch2); } }
Output:
567
5534.99
18765.567
2020
26
J
Java
true
55
null
₹
Backslash literal
?
Scope of the variable:
Local variables:
A variable that is declared within the method that is called local variables.
Instance variables
A non-static variable that is declared within the class but not in the method is
called an instance variable. Instance variables are related to a specific object.
Class variables
Class/Static variables A variable that is declared with static keyword inside a class is called static or class variable. They can be accessed by objects.
Example:
class A {
int amount = 100; //instance(object) variable
static int pin = 2115; //static(class) variable
public static void main(String[] args) {
int age = 35; //local(method) variable
System.out.println(age);
System.out.println(A.pin);
}
}
Review Questions
- What data always written in double quotes ( "")
- Class name should start with _______
- Why to use variables
- What datatype you used to store text data in java
- To store numbers, what datatype you use..
- What is the use of ++
- What is the default method in java give the name?
- What is the difference between char and string?
- what is comment in java
- How many types of comments are available in java
- variable can start with number?
- what symbols are allowed to use in variable name?
- what is literal?
- what are the escapee sequence characters in java ?give one example
- how to declare class variable in java?
- what is the use of static keyword in java
- where we use instance variables
- what is instance in java
- what is local variable, where it is declared
- what is use of args[] in main method
- how do you pass arugments to java program?
- what is difference between print and println
- how many bytes allocated for short datatype
- how many bytes taken by string
- how do you write float value in java and why?
Comments
Post a Comment