comp102 lec 3
TRANSCRIPT
![Page 1: Comp102 lec 3](https://reader034.vdocuments.net/reader034/viewer/2022052505/55620837d8b42acd4e8b597b/html5/thumbnails/1.jpg)
Introduction To Java
![Page 2: Comp102 lec 3](https://reader034.vdocuments.net/reader034/viewer/2022052505/55620837d8b42acd4e8b597b/html5/thumbnails/2.jpg)
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
![Page 3: Comp102 lec 3](https://reader034.vdocuments.net/reader034/viewer/2022052505/55620837d8b42acd4e8b597b/html5/thumbnails/3.jpg)
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
![Page 4: Comp102 lec 3](https://reader034.vdocuments.net/reader034/viewer/2022052505/55620837d8b42acd4e8b597b/html5/thumbnails/4.jpg)
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
![Page 5: Comp102 lec 3](https://reader034.vdocuments.net/reader034/viewer/2022052505/55620837d8b42acd4e8b597b/html5/thumbnails/5.jpg)
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
![Page 6: Comp102 lec 3](https://reader034.vdocuments.net/reader034/viewer/2022052505/55620837d8b42acd4e8b597b/html5/thumbnails/6.jpg)
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
![Page 7: Comp102 lec 3](https://reader034.vdocuments.net/reader034/viewer/2022052505/55620837d8b42acd4e8b597b/html5/thumbnails/7.jpg)
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
![Page 8: Comp102 lec 3](https://reader034.vdocuments.net/reader034/viewer/2022052505/55620837d8b42acd4e8b597b/html5/thumbnails/8.jpg)
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
}
![Page 9: Comp102 lec 3](https://reader034.vdocuments.net/reader034/viewer/2022052505/55620837d8b42acd4e8b597b/html5/thumbnails/9.jpg)
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.
![Page 10: Comp102 lec 3](https://reader034.vdocuments.net/reader034/viewer/2022052505/55620837d8b42acd4e8b597b/html5/thumbnails/10.jpg)
To RUN(Execute) Java Program
• java Program-name
![Page 11: Comp102 lec 3](https://reader034.vdocuments.net/reader034/viewer/2022052505/55620837d8b42acd4e8b597b/html5/thumbnails/11.jpg)
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.
![Page 12: Comp102 lec 3](https://reader034.vdocuments.net/reader034/viewer/2022052505/55620837d8b42acd4e8b597b/html5/thumbnails/12.jpg)
Java Data and Variables
• 8 primitive types– byte– short– int– long– float– double– char– boolean
![Page 13: Comp102 lec 3](https://reader034.vdocuments.net/reader034/viewer/2022052505/55620837d8b42acd4e8b597b/html5/thumbnails/13.jpg)
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
![Page 14: Comp102 lec 3](https://reader034.vdocuments.net/reader034/viewer/2022052505/55620837d8b42acd4e8b597b/html5/thumbnails/14.jpg)
Floating Point Primitive DataTypes
![Page 15: Comp102 lec 3](https://reader034.vdocuments.net/reader034/viewer/2022052505/55620837d8b42acd4e8b597b/html5/thumbnails/15.jpg)
Method to Declare variable
• Syntax– DataType VariableName;– Datatype VariableName = InitialValue;
• Examples– int yr = 2010;– double value = 89.12 ;
![Page 16: Comp102 lec 3](https://reader034.vdocuments.net/reader034/viewer/2022052505/55620837d8b42acd4e8b597b/html5/thumbnails/16.jpg)
Wrapper Class
![Page 17: Comp102 lec 3](https://reader034.vdocuments.net/reader034/viewer/2022052505/55620837d8b42acd4e8b597b/html5/thumbnails/17.jpg)
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
![Page 18: Comp102 lec 3](https://reader034.vdocuments.net/reader034/viewer/2022052505/55620837d8b42acd4e8b597b/html5/thumbnails/18.jpg)
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 ); }}
![Page 19: Comp102 lec 3](https://reader034.vdocuments.net/reader034/viewer/2022052505/55620837d8b42acd4e8b597b/html5/thumbnails/19.jpg)
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