Sei sulla pagina 1di 25

Introduction to Java

Getting started!

sudo apt-get update

sudo apt-get install default-jdk

https://www.eclipse.org/downloads/packages/
 Eclipse IDE for Java Developers

tar -xvzf eclipse-java-2019-06-R-linux-gtk-
x86_64.tar.gz

http://progrepo.blogspot.com/2012/10/eclipse-
unable-to-find-ant-file-to-run.html
First Java Program

public class hello {


public static void main(String args[]) {
System.out.println("This is my first java program");
}
}
Data Types


Primitive
 Integers

byte(8), short(16), int(32), long (64)
 Floating point

Float(32), double(64)
 Character

Char (16)
 Boolean

Boolean (8)

Non-primitive
 Arrays
Type conversion and casting

class Conversion {
public static void main(String args[]) {
byte b;
int i = 257;
double d = 323.142;
System.out.println("\nConversion of int to byte.");
b = (byte) i;
System.out.println("i and b " + i + " " + b);
System.out.println("\nConversion of double to int.");
i = (int) d;
System.out.println("d and i " + d + " " + i);
System.out.println("\nConversion of double to byte.");
b = (byte) d;
System.out.println("d and b " + d + " " + b);
}
}
1D array
// Demonstrate a one-dimensional array.
class Array {
public static void main(String args[]) {
int month_days[];
month_days = new int[12];
month_days[0] = 31;
month_days[1] = 28;
month_days[2] = 31;
month_days[3] = 30;
month_days[4] = 31;
month_days[5] = 30;
month_days[6] = 31;
month_days[7] = 31;
month_days[8] = 30;
month_days[9] = 31;
month_days[10] = 30;
month_days[11] = 31;
System.out.println("April has " + month_days[3] + " days.");
}
}
1D array (alternative)
class AutoArray {
public static void main(String args[]) {
int month_days[] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
System.out.println("April has " + month_days[3] + " days.");
}
}
#############################
// Average an array of values.
class Average {
public static void main(String args[]) {
double nums[] = {10.1, 11.2, 12.3, 13.4, 14.5};
double result = 0;
int i;
for(i=0; i<5; i++)
result = result + nums[i];
System.out.println("Average is " + result / 5);
}
}
2D array
// Demonstrate a two-dimensional array.
class TwoDArray {
public static void main(String args[]) {
int twoD[][]= new int[4][5];
int i, j, k = 0;
for(i=0; i<4; i++)
for(j=0; j<5; j++) {
twoD[i][j] = k;
k++;
}
for(i=0; i<4; i++) {
for(j=0; j<5; j++)
System.out.print(twoD[i][j] + " ");
System.out.println();
}
}
}
2D array (cont)
// Manually allocate differing size second dimensions.
class TwoDAgain {
public static void main(String args[]) {
int twoD[][] = new int[4][];
twoD[0] = new int[1];
twoD[1] = new int[2];
twoD[2] = new int[3];
twoD[3] = new int[4];
int i, j, k = 0;
for(i=0; i<4; i++)
for(j=0; j<i+1; j++) {
twoD[i][j] = k;
k++;
}
for(i=0; i<4; i++) {
for(j=0; j<i+1; j++)
System.out.print(twoD[i][j] + " ");
System.out.println();
}
}
}
Operators


Arithmetic

Bitwise

Relational

Boolean

Assignment

?
Control Statements


if-else

Nested if

The if-else-if Ladder

switch

Nested switch

while

do-while

for

continue

break

Java Fundamentals


Java supports the following fundamental
concepts
 Classes
 Objects
 Polymorphism
 Inheritance
 Encapsulation
 Abstraction
 Instance
 Method
 Message Passing
Java – Classes and objects

A class is a user defined blueprint or prototype from which objects are created.


It represents the set of properties or methods that are common to all objects of
one type.


In general, class declarations can include these components, in order:

 Modifiers : A class can be public or has default access (Refer this for details).

Access Modifiers − default, public , protected, private

Non-access Modifiers − final, abstract, strictfp
 Class name: The name should begin with a initial letter (capitalized by
convention).
 Superclass (if any): The name of the class’s parent (superclass), if any,
preceded by the keyword extends. A class can only extend (subclass) one
parent.
 Interfaces (if any): A comma-separated list of interfaces implemented by the
class, if any, preceded by the keyword implements. A class can implement
more than one interface.
 Body: The class body surrounded by braces, { }.

Every Java program has atleast one class and one main method.
General Form of Class
class classname {
type instance-variable1;
type instance-variable2;
// ...
type instance-variableN;
type methodname1(parameter-list) {
// body of method
}
type methodname2(parameter-list) {
// body of method
}
// ...
type methodnameN(parameter-list) {
// body of method
}
}

The data, or variables, defined within a class are called instance variables.

The code is contained within methods.

Collectively, the methods and variables defined within a class are called
members of the class.
/* A program that uses the Box class.

Call this file BoxDemo.java
*/

class Box {
 double width;
double height;
double depth;
}

// This class declares an object of type Box.


class BoxDemo {
public static void main(String args[]) {
Box mybox = new Box();
double vol;
// assign values to mybox's instance variables
mybox.width = 10;
mybox.height = 20;
mybox.depth = 15;
// compute volume of box
vol = mybox.width * mybox.height * mybox.depth;
System.out.println("Volume is " + vol);
}
}
// This program declares two Box objects.
class Box {
double width;
double height;
double depth;
}
class BoxDemo2 {
public static void main(String args[]) {
Box b1 = new Box();
Box b2 = new Box();
double vol;
// assign values to mybox1's instance variables
b1.width = 10;
b1.height = 20;
b1.depth = 15;
/* assign different values to mybox2's instance variables */
b2.width = 3;
b2.height = 6;
b2.depth = 9;
// compute volume of first box
vol = b1.width * b1.height * b1.depth;
System.out.println("Volume is " + vol);
// compute volume of second box
vol = b2.width * b2.height * b2.depth;
System.out.println("Volume is " + vol);
}
Constructors
/* Here, Box uses a constructor to initialize the
dimensions of a box. */
class Box {

A constructor initializes double width;
an object immediately double height;
double depth;
upon creation. // This is the constructor for Box.

It has the same name Box() {
as the class in which it System.out.println("Constructing Box");
width = 10;
resides and is height = 10;
syntactically similar to a depth = 10;
}
method. // compute and return volume

Constructors have no double volume() {
return width * height * depth;
return type, not even }
void. }

It is the constructor’s class BoxDemo {
public static void main(String args[]) {
job to initialize the // declare, allocate, and initialize Box objects
internal state of an Box mybox1 = new Box();
Box mybox2 = new Box();
object so that the code double vol;
creating an instance will // get volume of first box
vol = mybox1.volume();
have a fully initialized, System.out.println("Volume is " + vol);
usable object // get volume of second box
immediately. vol = mybox2.volume();
System.out.println("Volume is " + vol);
}
}
Parameterized Constructors
/* Here, Box uses a parameterized constructor to
initialize the dimensions of a box.*/

The following class Box {
double width;
version of Box double height;
double depth;
defines a // This is the constructor for Box.
parameterized Box(double w, double h, double d) {
width = w;
constructor that height = h;
depth = d;
sets the }
// compute and return volume
dimensions of a double volume() {
return width * height * depth;
box as specified }
}
by those class BoxDemo {
public static void main(String args[]) {
parameters. // declare, allocate, and initialize Box objects
Box mybox1 = new Box(10, 20, 15);
Box mybox2 = new Box(3, 6, 9);
double vol;
// get volume of first box
vol = mybox1.volume();
System.out.println("Volume is " + vol);
// get volume of second box
vol = mybox2.volume();
System.out.println("Volume is " + vol);
}
}
The this Keyword

“this” is used inside a method to refer to the
current object.

That is, this is always a reference to the object
on which the method was invoked.

Consider the following version of Box( ):

// A redundant use of this.
Box(double w, double h, double d) {
this.width = w;
this.height = h;
this.depth = d;
}

Write a program in Java which describes a
class student using default and parametrized
constructors. It should have instance variables
to record the id, name and age of a student.
Create two student objects and display their
details.
public class Student {
int id;
String name;
int age;
// Default constructor
Student() {
this.id = 0;
this.name = "no data";
this.age = 1;
}
// Parameterized constructor
Student(int i, String n, int a) {
this.id = i;
this.name = n;
this.age = a;
}
}
public class MyClass {
public static void main(String args[]) {
Student jenny = new Student();
Student tracy = new Student(1, “tracy”, 20);
System.out.println(jenny.getName() + " is " +
jenny.getAge() + " years old ");
System.out.println(tracy.getName() + " is " +
tracy.getAge() + " years old ");
}
}

Potrebbero piacerti anche