13
Topics JAVA Compilation and Interpretation JAVA Platform Independence Building First JAVA Program Escapes Sequences Display text with printf Data types in Java Constants and Variables

JAVA Compilation and Interpretation JAVA Platform Independence Building First JAVA Program Escapes Sequences Display text with printf Data

Embed Size (px)

Citation preview

Page 1: JAVA Compilation and Interpretation  JAVA Platform Independence  Building First JAVA Program  Escapes Sequences  Display text with printf  Data

Topics

JAVA Compilation and Interpretation JAVA Platform Independence Building First JAVA Program Escapes Sequences Display text with printf Data types in Java Constants and Variables

Page 2: JAVA Compilation and Interpretation  JAVA Platform Independence  Building First JAVA Program  Escapes Sequences  Display text with printf  Data

JAVA Compilation and Interpretation

Text Editor/ IDE

Compiler Interpreter

Programmer

Source Code

.java file

Byte Code

.class file

Hardware and Operating System

Notepad, emacs,vi, Netbeans, Eclipse etc.,

javac

javaappletviewer

Page 3: JAVA Compilation and Interpretation  JAVA Platform Independence  Building First JAVA Program  Escapes Sequences  Display text with printf  Data

JAVA Platform Independence

JAVA COMPILER

JAVA BYTE CODE

JAVA INTERPRETER

Windows Macintosh Solaris Windows NT

(translator)

(same for all platforms)

(one for each different system)

Page 4: JAVA Compilation and Interpretation  JAVA Platform Independence  Building First JAVA Program  Escapes Sequences  Display text with printf  Data

JAVA Platform Independence

Java Compiler - Java source code (file with extension .java) to bytecode (file with extension .class)

Bytecode - an intermediate form, closer to machine representation

A interpreter (virtual machine) on any target platform interprets the bytecode.

Page 5: JAVA Compilation and Interpretation  JAVA Platform Independence  Building First JAVA Program  Escapes Sequences  Display text with printf  Data

Building First JAVA Program

//This program prints Welcome to Java! public class Welcome { public static void main(String[] args) { System.out.printf(“%s”, “Welcome to Java!”); }}

Welcome.java

Page 6: JAVA Compilation and Interpretation  JAVA Platform Independence  Building First JAVA Program  Escapes Sequences  Display text with printf  Data

Building First JAVA Program

Trace a Program Execution

//This program prints Welcome to Java! public class Welcome { public static void main(String[] args) { System.out.printf(“%s”, “Welcome to Java!"); }}

First line indicates comment. Which is not compiled by the JAVAPublic Specifies that the method is accessible from anywhere Class is keyword and Welcome is a java identifier that specifies the name of

the class to be defined. Static is keyword, the main must always be declared as static since the

interpreter uses this method before any objects are created. Void is an modifier that states the main method does not return any value. This is similar to the printf() statement of C.

Page 7: JAVA Compilation and Interpretation  JAVA Platform Independence  Building First JAVA Program  Escapes Sequences  Display text with printf  Data

Escape Sequences

Escape sequence

Description

\n Newline. Position the screen cursor at the beginning of the next line.

\t Horizontal tab. Move the screen cursor to the next tab stop. \r Carriage return. Position the screen cursor at the beginning of the

current line—do not advance to the next line. Any characters output after the carriage return overwrite the characters previously output on that line.

\\ Backslash. Used to print a backslash character. \" Double quote. Used to print a double-quote character. For example,

System.out.println( "\"in quotes\"" );

displays

"in quotes"

Page 8: JAVA Compilation and Interpretation  JAVA Platform Independence  Building First JAVA Program  Escapes Sequences  Display text with printf  Data

Display Text with printf

System.out.printf

Feature added in Java SE 5.0Displays formatted data

Format string Fixed text Format specifier – placeholder for a value

Format specifier %s – placeholder for a string

System.out.printf( "%s\n%s\n", "Welcome to", "Java Programming!" );

Page 9: JAVA Compilation and Interpretation  JAVA Platform Independence  Building First JAVA Program  Escapes Sequences  Display text with printf  Data

Data types in JAVA

Although complex data values are represented using objects, Java defines a set of primitive types to represent simple data.

Java provides eight primitive data types, they are boolean char, byte, short, int, long float, double

Primitive Data Types

Size in Bytes

Size in Bits

int 4 32

long 8 64

float 4 32

double 8 64

char 2 16

boolean (boolean in Java)

1 8

Page 10: JAVA Compilation and Interpretation  JAVA Platform Independence  Building First JAVA Program  Escapes Sequences  Display text with printf  Data

Primitive Data types in JAVA

A data type is defined by a set of values called the domain and a set of operations. The following table shows the data domains and common operations for all eight of Java’s primitive types:

Type

short

int

long

float

double

char

boolean

8-bit integers in the range –128 to 127

16-bit integers in the range –32768 to 32767

32-bit integers in the range–2146483648 to 2146483647

64-bit integers in the range–9223372036754775808 to 9223372036754775807

32-bit floating-point numbers in the range± 1.4 x 10-45 to ± 3.4028235 x 10-38

64-bit floating-point numbers in the range± 4.39 x 10-322 to ± 1.7976931348623157 x 10308

16-bit characters encoded using Unicode

the values true and false

The arithmetic operators:+-

*/%

addsubtract

remainderdividemultiply

= =<

!=<=>=

equal toless than

greater or equalless or equalnot equal

> greater than

The arithmetic operators except %

The relational operators:

The relational operators

The relational operators

The logical operators:&& add || or ! not

Domain Common operations

byte

Page 11: JAVA Compilation and Interpretation  JAVA Platform Independence  Building First JAVA Program  Escapes Sequences  Display text with printf  Data

Constants and Variables

The simplest terms that appear in expressions are constants and variables. The value of a constant does not change during the course of a program. A variable is a name for memory location, in which value can be stored this value may be changed at the time of execution.

Each variable has the following attributes: A name, which enables you to differentiate one variable from another. A type, which specifies what type of value the variable can contain. A value, which represents the current contents of the variable.

A variable in Java is most easily envisioned as a box capable of storing a value.

total

42

Page 12: JAVA Compilation and Interpretation  JAVA Platform Independence  Building First JAVA Program  Escapes Sequences  Display text with printf  Data

Variable Declaration In Java, you must declare a variable before you can use it. The declaration

establishes the name and type of the variable and, in most cases, specifies the initial value as well.

type name = value;

The most common form of a variable declaration is

where type is the name of a Java primitive type or class, name is an identifier that indicates the name of the variable, and value is an expression specifying the initial value.

RULES:1. First character should be an alphabet and remaining characters can be alpha-numeric.

2. Keywords cannot be used for defining.

3. No special symbols are allowed expect underscore and dollar.

4. Java is case sensitive, “Num” and “NUM” are different.

Page 13: JAVA Compilation and Interpretation  JAVA Platform Independence  Building First JAVA Program  Escapes Sequences  Display text with printf  Data

?