You are on page 1of 29

Language Basics

DAC – Aug 2008


Objectives
At the end of this session, you will be able to:
Compile and execute the first HelloWorld program in java
State the data types available in java
Using arrays
DAC – Aug 2008
The Environment

Runtime

Class Loader

Hello.java Bytecode Verifier

javac Hello.java Interpreter


Network
DAC – Aug 2008

Hardware
Hello.class
Sample Code
Hello.java

public class Hello {


public static void main(String [] args) {
System.out.println(“Hello World”);
}
}

Compilation - javac Hello.java


Execution - java Hello
DAC – Aug 2008
Language Basics
Keywords
Variables
Conditional Statements
Loops
Data Types
Operators
Coding Conventions
DAC – Aug 2008
Java Keywords
abstract boolean break byte case catch

char class const continue default do

double else extends final finally float

for goto if implements import instanceof

int interface long native new package

private protected public return short static

strictfp super switch synchronized this throw

throws transient try void volatile while


DAC – Aug 2008

true false null


Data types In Java
Primitive Data Types

Boolean type Numeric type

boolean
Integral Floating
Type type
CharactyerType Integer
Type
DAC – Aug 2008

char byte short int long float double


Data Types
Integral Type
byte 8 bits
short 16 bits
int 32 bits
long 64 bits
Textual Type
char 16 bits, UNICODE Character
String
DAC – Aug 2008
Data Types
Floating Point Type
float 32 bits
double 64 bits
Boolean Type
true
false
DAC – Aug 2008
Reference Types

Reference

interface types Class types

array types
DAC – Aug 2008
Variables
A variable is a name given memory location. That memory is
associated to a data type and can be assigned a value
int n;
float f1;
char ch;
double d;
DAC – Aug 2008
Variables conti…
Assigning a value to a variable
Initialization of a variable with a primary value

4. int n1;
5. n1 =21 ; // assignment
6. int i2 = 18; // initialization
7. char ch = ‘S’; // initialization
8. double d = 21.8; // initialization
9. d = n1; // assignment
10. float f1 = 16.13F;
DAC – Aug 2008
DAC – Aug 2008

Arrays
Declaring Arrays
Group data objects of the same type
Declare arrays of primitive or class types
char s[]; or char [] s;
Point p[]; or Point [] p;
Create space for reference.
An array is an object, not memory reserved for primitive
types
DAC – Aug 2008
Creating Arrays
Use the new keyword to create an array object
s = new char[20];
p = new Point[100];

p[0] = new Point();


p[1] = new Point();
.
.
.
DAC – Aug 2008
Initializing Arrays
Initialize an array element
Create an array with initial values
String names[] = new String[3];
names [0] = “Jack”;
names [1] = “Jill”;
names [2] = “Tom”;

MyClass array[] = {new MyClass(), new MyClass() };


DAC – Aug 2008
Multi-Dimensional Arrays
Arrays of arrays :
int twoDim [] [] = new int [4] [];
twoDim [0] = new int [5];
twoDim [1] = new int [5];

int twoDim [] [] = new int [] [5]; // illegeal


DAC – Aug 2008
Multi-Dimensional Arrays
Non-Rectangular arrays of arrays
twoDim [0] = new int[17];
twoDim [1] = new int[2];
twoDim [2] = new int[11];
twoDim [3] = new int[7];
Array of four arrays of five integers each :
int twoDim[] [] = new int[4][5];
DAC – Aug 2008
Array Bounds
All array subscripts begin with 0 :

int list [] = new int[10];


for (int i = 0; i< list.length; i++) {
System.out.println(list[i]);
}
DAC – Aug 2008
Array Resizing
Can not resize an array
Can use the same reference variable to refer to an entirely new
array
int elements [] = new int[5];
elements = new int[10];
DAC – Aug 2008
What will be the answer?
int arr[][]={{12,3,4},{11,5,6},{5,6,7}};
arr[0][2]=?
arr[1][2]=?
arr[2][2]=?
DAC – Aug 2008
Packages
Packages are used to group related classes and interfaces
Classes with same name can be put into different packages
A package is a collection of related types providing access
protection
DAC – Aug 2008
Creating package

To create a package, you put a type (class, interface) in it


To do this, a package statement at the top of the source file
in which the type is defined
For example, the following code appears in the source file
pkg1.java and puts the SupA class in the pkg1 package:
DAC – Aug 2008
Create a package
package pkg1;
public class Emp
{ String name;
Emp()
{
name=“anni”;
}
void ShowNm()
{ System.out.println(“Hello”+name);
}
}
DAC – Aug 2008
Using package
import pkg1.*;

class AnyClass
{
public static void mian(String a[])
{
Emp e1=new Emp();
e1.showNm();
}
}
DAC – Aug 2008
Finding Packages and CLASSPATH
How does the Java run-time system know where to look for
packages that you create?
Packages are like directories
You can specify a directory path or paths by setting the
CLASSPATH environmental variable
DAC – Aug 2008
Access Modifiers

Modifier Public Private Protected Friendly

Same Class Yes Yes Yes Yes


Child Class Yes No Yes Yes
Same Package
Diff. Class Yes No Yes Yes
Same Package

Child Class Yes No Yes No


Diff Package

Diff. Class Yes No No No


DAC – Aug 2008

Diff Package
An Access Modifier Example
Create a package having a class , which will hold
public,private,default,protected field members
Create another package having Test class which will access
above package and class members
Which members you will be able to call
DAC – Aug 2008
Comments
There are three permissible styles of inserting comments in
Java
// Comment in one line

/* Comment in one or
more lines */

/** Documentation comment */


DAC – Aug 2008

You might also like