Chapter 1- Introduction to Java

Contents

SETTING UP JAVA.. 2

Introduction. 3

The Way Java Works. 4

What you’ll do in Java. 4

Java History. 6

Code Structure in Java. 7

Anatomy of a class. 8

Simple Statements. 10

Declaration. 10

Arithmetic. 10


SETTING UP JAVA

  1. U need 5 or greater Java Standard Edition SDK (Software Development Kit). If you’re on Linux, Windows, or Solaris, you can download it for free from Oracle website. Get the latest non-beta version posted. The SDK includes everything you need to compile and run Java. On Mac OS Java SDK is already installed.

N.B.: Java 1.5 and Java 5 are same. Sun directly jumped from Java 1.4 to Java 5! But SDK version was kept as 1.5.

  1. Download J2SE API Documentation. It’s not included in SDK. It’s available online as well so u can skip this if u have internet!
  2. Text Editor (a simple Notepad) is enough. Other examples are VI, Emacs, and Gedit.
  3. Once you’ve downloaded and unpacked/zipped/etc; you need to add an entry to your PATH environment variable that points to the /bin directory inside the main Java directory. Give it a test!

Using an IDE (like Eclipse or Netbeans) is not recommended for learning Java. An IDE does automate some of the process for u. U must learn all those before moving to IDE!

We use simple UML-like diagrams

 

We will do organizing and packaging later


Introduction

Distinguishing features of Java which made it so popular:

  • Object Oriented
  • Memory Management
  • Portability – write-once/run-anywhere

Huge following of Java even though it was (and is still) slower!

Java Compared to other languages

Not to mention it has grown to a very rich library support!


The Way Java Works

What you’ll do in Java

  1. Type a source code file

(Extension of source file is .java. e.g. Hello.java)

  1. Compile it (using the javac compiler)

javac Hello.java. If there r no errors u will get output Hello.class (bytecode)

  1. Run the compiled bytecode on a Java virtual machine.

java Hello

Java supports the writing of many different kinds of executables: applications, applets, and servlets.

Java is both a compiled and an interpreted language.

javac – Java Compiler

java – Java Interpreter / JVM

Java is Platform Independent – Role of JVM and usage of Bytecode ***

If u just want to run Java Applications, u need JRE (Java Runtime Environment)

But if u want to write (code) Java Applications u need Java SE SDK (or JDK)

JVM or Java Interpreter                (java)                    JRE (JVM + Standard classes)

JDK (JRE + developer tools like Java Compiler – javac)

Java History

Code Structure in Java

We can have multiple sources files in one java application. But for beginning we will just create single source file applications.

 

 

 

Anatomy of a class

When the JVM starts running, it looks for the class u give it at the command line. Then it starts looking for a specially written method that looks exactly like:

public static void main (String[] args)

This is the Entry Point to you application

 

print vs. println – println adds new line character.

To print values of variable:

int i=10;

System.out.println(“value of I is “ + i).

(no formatting required)

Running a program means telling the JVM to “Load the Hello class, then start executing its main() method. Keep running till all the code in main() is finished.”

Inside main() or any other method of any class, u put code similar to other programming languages like

  1. Simple Statements (declarations, assignments, arithmetic, method calls etc)
  2. Loops (for and while)
  3. Branching (if-else)

public static void main(String[] args) is the correct way of defining main.

The placement of the keywords public and static is interchangeable. So

static public void main(String[] args) is VALID.

Omitting (or changing) any of public, static, void and param will not cause compilation error but JVM will not find the entry point to the program.

Simple Statements

Declaration

Variable declaration (*** variable is like a container)

type name;

Basic Data Types – int, float, character

                E.g.         int loanAmount;               float interest2Pay;           char card_type;

*** char variable can store only one character (alphanumeric + others) ***

Literals (constants)

10 (int literal)     5.235 (float literal)           ‘Q’ (char literal)

= is assignment operator

int loanAmount;

loanAmount = 10000;

float interest2Pay;

interest2Pay = 12.36;

char card_type;

card_type = ‘Q’;

Combine declaration and assignment:

int loanAmount = 10000;

float interest2Pay = 12.36;

char card_type = ‘Q’;

Combine multiple declarations:

int loanAmount = 10000, loadPeriod = 3;

float interest2Pay = 12.36, height=1.75;

char card_type = ‘Q’, currency=‘$’;

Arithmetic

*, /, %, -, + are the arithmetic operators

E.g.         int i = 5 + 3 / 2 – 1 * 2;

float area = length * breadth;

Leave a comment