



Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
fgr rkogtjr ggrret re trertr tertr
Typology: Exercises
1 / 6
This page cannot be seen from the preview
Don't miss anything!




data type variable [ = value][, variable [ = value] ...] ;
data type variable [ = value][, variable [ = value] ...] ;
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
b
// Example of initialization
// Example of initialization
byte
byte 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' ;
// the char variable a iis initialized with value 'a'
// the char variable a iis initialized with value 'a'
ExampleExample
public public classclass TestTest {{
public
public void
void pupAge
pupAge ()
int
int 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
new
new Test
Test ();
test
test .
pupAge
pupAge ();
Output
Output
Puppy age is: 7 Puppy age is: 7
Example
Example
public
public class
class Test
Test {
public public voidvoid pupAgepupAge()() {{
Live Demo
Live Demo
Live Demo
Live Demo
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
empName
empName ;
// The salary variable is assigned a value.
// The salary variable is assigned a value.
public public voidvoid setSalarysetSalary((doubledouble empSalempSal)) {{
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
new
new Employee
Employee (
"Ransika"
"Ransika" );
empOne empOne..setSalarysetSalary(( 10001000 ););
empOne
empOne .
printEmp
printEmp ();
Live Demo
Live Demo
Output
Output
name name : Ransika: Ransika
salary :1000.
salary :1000.
Class/Static Variables
Class/Static 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
"Development "
"Development " ;
public
public static
static void
void main
main (
String
String args
args [])
Live Demo
Live Demo