Comp102 lec 3
-
Upload
fraz-bakhsh -
Category
Documents
-
view
167 -
download
2
Transcript of Comp102 lec 3
Introduction To Java
Java
• Programming Language• Developed by: James Gosling at Sun
Microsystems in 1991 , referred as OAK• Released in 1995 as core component of Sun
Microsystems Java Platform• Derives much of its syntax from C and C++• Applications are compiled to bytecode that can
run on any JVM( Java Virtual Machine) regardless of computer architecture
Based on material from: http://en.wikipedia.org/wiki/Java_%28programming_language%29
Java Virtual Machine• A virtual machine (VM) is a software implementation of a
machine that executes programs like a physical machine1
• JVM enables a set of computer software programs and data structures to use a virtual machine model for the execution of other computer programs and scripts.2
• The model used by a JVM accepts a form of computer intermediate language commonly referred to as Java bytecode.2
• Java bytecode instructions are analogous to machine code, but are intended to be interpreted by a virtual machine
Based on material from: 1. http://en.wikipedia.org/wiki/Virtual_machine2.http://en.wikipedia.org/wiki/Java_Virtual_Machine
Java is:• General-purpose• Concurrent• class-based and• Object-oriented, • Is specifically designed to have as few implementation
dependencies as possible.• It is intended to let application developers "write once,
run anywhere". • Java is considered by many as one of the most influential
programming languages of the 20th century, and widely used from application software to web application
Based on material from: http://en.wikipedia.org/wiki/Java_%28programming_language%29
Principles
• There were five primary goals in the creation of the Java language:– It should be "simple, object oriented, and
familiar".– It should be "robust and secure".– It should be "architecture neutral and portable".– It should execute with "high performance".– It should be "interpreted, threaded, and
dynamic".
Design Goals of the JavaTM Programming Language
Object Oriented• A technique or way to model real world
problems• Many interacting objects exist• Objects interact via messages• Class Vs Object– A class is a user defined data type– Object is any instance of class
Object• The fundamental idea behind object-oriented
language is to combine into a single unit both– data and– the functions that operate on that data.– Such a unit is called an object
• Five basic properties – Identity– Attributes– Behavior– Interface– Life Span
Java Program• A java program is defined by a public class that takes
the form:public class program-name {
optional variable declarations and methods public static void main(String[] args)
{ statements
} optional variable declarations and methods
}
To compile Java Code
• javac program-name.java• For this to work, the javac must be in your shell's path
or you must explicitly specify the path to the program (such as c:\j2se\bin\javac program-name.java).
• If the compilation is successful, javac will quietly end and return you to a command prompt.
• If you look in the directory, there will now be a program-name.class file.
• This file is the compiled version of your program. Once your program is in this form, its ready to run.
• If not, or you receive an error message, check for typographical errors in your source code.
To RUN(Execute) Java Program
• java Program-name
Java Comments• The Java programming language supports three kinds of
comments:• /* text */
The compiler ignores everything from /* to */. • /** documentation */
This indicates a documentation comment (doc comment, for short). The compiler ignores this kind of comment, just like it ignores comments that use /* and */. The JDK javadoc tool uses doc comments when preparing automatically generated documentation.
• // textThe compiler ignores everything from // to the end of the line.
Java Data and Variables
• 8 primitive types– byte– short– int– long– float– double– char– boolean
Integer Primitive Data Types
Type Size Range
byte 8 bits -128 to +127
short 16 bits -32,768 to +32,767
int 32 bits (about)-2 billion to +2 billion
long 64 bits (about)-10E18 to +10E18
Floating Point Primitive DataTypes
Method to Declare variable
• Syntax– DataType VariableName;– Datatype VariableName = InitialValue;
• Examples– int yr = 2010;– double value = 89.12 ;
Wrapper Class
Variable• Variables only exist within the structure in which
they are defined. • For example, if a variable is created within a
method, it cannot be accessed outside the method. • In addition, a different method can create a variable
of the same name which will not conflict with the other variable.
• A java variable can be thought of as a little box made up of one or more bytes that can hold a value of a particular data type
Exampleclass example{ public static void main ( String[] args ) { long x = 123; //a declaration of a variable named x with a datatype of long
System.out.println("The variable x has: " + x ); }}
Java Command Line Arguments
• Arguments are passed as a String array to the main method of a class.
• The first element (element 0) is the first argument passed not the name of the class