Comp102 lec 3


Published on

  • Be the first to comment

  • Be the first to like this

No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide

Comp102 lec 3

  1. 1. Introduction To Java
  2. 2. 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 architectureBased on material from:
  3. 3. 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 machineBased on material from:1.
  4. 4. 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 applicationBased on material from:
  5. 5. 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
  6. 6. 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
  7. 7. 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
  8. 8. 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}
  9. 9. To compile Java Code• javac• For this to work, the javac must be in your shells path or you must explicitly specify the path to the program (such as c:j2sebinjavac• 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.
  10. 10. To RUN(Execute) Java Program• java Program-name
  11. 11. 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.• // text The compiler ignores everything from // to the end of the line.
  12. 12. Java Data and Variables• 8 primitive types – byte – short – int – long – float – double – char – boolean
  13. 13. Integer Primitive Data TypesType Size Rangebyte 8 bits -128 to +127 -32,768 toshort 16 bits +32,767 (about)-2 billionint 32 bits to +2 billion (about)-10E18 tolong 64 bits +10E18
  14. 14. Floating Point Primitive DataTypes
  15. 15. Method to Declare variable• Syntax – DataType VariableName; – Datatype VariableName = InitialValue;• Examples – int yr = 2010; – double value = 89.12 ;
  16. 16. Wrapper Class
  17. 17. 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
  18. 18. 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 ); }}
  19. 19. 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