Programming in java Lecture-2 Java Introduction. Platform Computing platform include hardware and software framework, this combination allows software.

Slides:



Advertisements
Similar presentations
IT 325 OPERATING SYSTEM C programming language. Why use C instead of Java Intermediate-level language:  Low-level features like bit operations  High-level.
Advertisements

Core Java Lecture 4-5. What We Will Cover Today What Are Methods Scope and Life Time of Variables Command Line Arguments Use of static keyword in Java.
C++ Basics March 10th. A C++ program //if necessary include headers //#include void main() { //variable declaration //read values input from user //computation.
Object Oriented Programming in JAVA
1 Java Basics. 2 Compiling A “compiler” is a program that translates from one language to another Typically from easy-to-read to fast-to-run e.g. from.
Java Intro. A First Java Program //The Hello, World! program in Java public class Hello { public static void main(String[] args) { System.out.println("Hello,
CS31: Introduction to Computer Science I Discussion 1A 4/2/2010 Sungwon Yang
Introduction to Java CS 331. Introduction Present the syntax of Java Introduce the Java API Demonstrate how to build –stand-alone Java programs –Java.
CS107 Introduction to Computer Science Java Basics.
CMSC 341 Introduction to Java Based on tutorial by Rebecca Hasti at
C++ Basics CSci 107. A C++ program //include headers; these are modules that include functions that you may use in your //program; we will almost always.
Programming in Java; Instructor:Moorthy Introduction, Objects, Classes, Libraries1 Programming in Java Introduction.
Introducing Java.
© The McGraw-Hill Companies, 2006 Chapter 1 The first step.
Introduction to Java Appendix A. Appendix A: Introduction to Java2 Chapter Objectives To understand the essentials of object-oriented programming in Java.
DAT602 Database Application Development Lecture 5 JAVA Review.
CS107 Introduction to Computer Science Java Basics.
1 Module Objective & Outline Module Objective: After completing this Module, you will be able to, appreciate java as a programming language, write java.
Chapter 1: Introducing JAVA. 2 Introduction Why JAVA Applets and Server Side Programming Very rich GUI libraries Portability (machine independence) A.
The Java Programming Language
INTRODUCTION TO JAVA. Introduction to Java Java programming language ◦ Object-Oriented Programming ◦ Compiled to byte code of JVM Java virtual machine.
CS 11 java track: lecture 1 Administrivia need a CS cluster account cgi-bin/sysadmin/account_request.cgi need to know UNIX
© 2012 Pearson Education, Inc. All rights reserved. 1-1 Why Java? Needed program portability – Program written in a language that would run on various.
Geoff Holmes and Bernhard Pfahringer COMP206-08S General Programming 2.
Java Programming, Second Edition Chapter One Creating Your First Java Program.
Week 1 - Friday.  What did we talk about last time?  Our first Java program.
Java means Coffee Java Coffee Beans The name “JAVA” was taken from a cup of coffee.
Basics of Java IMPORTANT: Read Chap 1-6 of How to think like a… Lecture 3.
2: Everything is an Object You Manipulate Objects Using References Primitives Arrays in Java Scoping You Never Destroy Objects Creating New Data Types:
Introduction to java Prepared By:-Pragnesh Patel Lect. In Computer Dept. NSIT,Jetalpur 1.
Netprog: Java Intro1 Crash Course in Java. Netprog: Java Intro2 Why Java? Network Programming in Java is very different than in C/C++ –much more language.
CHAPTER 3 GC Java Fundamentals. 2 BASICS OF JAVA ENVIRONMENT  The environment  The language  Java applications programming Interface API  Various.
 JAVA Compilation and Interpretation  JAVA Platform Independence  Building First JAVA Program  Escapes Sequences  Display text with printf  Data.
FIRST JAVA PROGRAM. JAVA PROGRAMS Every program may consist of 1 or more classes. Syntax of a class: Each class can contain 1 or more methods. public.
Using Data Within a Program Chapter 2.  Classes  Methods  Statements  Modifiers  Identifiers.
 Pearson Education, Inc. All rights reserved Introduction to Java Applications.
1 Programming Java Java Basics. 2 Java Program Java Application Program Application Program written in general programming language Applet Program running.
Fall 2015CISC124 - Prof. McLeod1 CISC124 Have you filled out the lab section survey? (As of last night 54 left to fill out the survey.) TA names have been.
Getting Started Java Fundamentals CSC207 – Software Design Summer 2011 – University of Toronto – Department of Computer Science.
Everything is an object (CH-2) Manipulating Objects with References. Manipulating Objects with References. String s; String s = “IS2550” String s = new.
Core Java Introduction Byju Veedu Ness Technologies httpdownload.oracle.com/javase/tutorial/getStarted/intro/definition.html.
1 Basic Java Constructs and Data Types – Nuts and Bolts Looking into Specific Differences and Enhancements in Java compared to C.
Developed at Sun Microsystems in 1991 James Gosling, initially named “OAK” Formally announced java in 1995 Object oriented and cant write procedural.
© 2012 Pearson Education, Inc. All rights reserved types of Java programs Application – Stand-alone program (run without a web browser) – Relaxed.
introductory lecture on java programming
Spring 2009 Programming Fundamentals I Java Programming XuanTung Hoang Lecture No. 8.
Java Computer Industry Lab. 1 Programming Java Java Basics Incheon Paik.
ITP 109 Week 2 Trina Gregory Introduction to Java.
Review A program is… a set of instructions that tell a computer what to do. Programs can also be called… software. Hardware refers to… the physical components.
Features of JAVA PLATFORM INDEPENDENT LANGUAGE JAVA RUNTIME ENVIRONMENT (JRE) JAVA VIRTUAL MACHINE (JVM) JAVA APP BYTE CODE JAVA RUNTIME ENVIRONMENT.
Object Oriented Programming Lecture 2: BallWorld.
SESSION 1 Introduction in Java. Objectives Introduce classes and objects Starting with Java Introduce JDK Writing a simple Java program Using comments.
CPSC 233 Tutorial January 21 st /22 nd, Linux Commands.
 It is a pure oops language and a high level language.  It was developed at sun microsystems by James Gosling.
Sung-Dong Kim, Dept. of Computer Engineering, Hansung University Java - Introduction.
Introduction CMSC 202 Fall Instructors Mr. Ryan Bergeron – Lecture Section 01 Tues/Thu 1:00 – 2:15 am, Sondheim 111 – Lecture Section 04 Tues/Thu.
Basic concepts of C++ Presented by Prof. Satyajit De
JAVA MULTIPLE CHOICE QUESTION.
GC101 Introduction to computer and program
Chapter No. : 1 Introduction to Java.
Internet and Java Foundations, Programming and Practice
Data types and variables
Java programming lecture one
Programming Language Concepts (CIS 635)
Object Oriented Programming
Java Intro.
Program Breakdown, Variables, Types, Control Flow, and Input/Output
C++ Basics CSci 107. A C++ program //include headers; these are modules that include functions that you may use in your //program; we will almost always.
F II 2. Simple Java Programs Objectives
Computer Programming-1 CSC 111
Presentation transcript:

Programming in java Lecture-2 Java Introduction

Platform Computing platform include hardware and software framework, this combination allows software to run.

Platform problem Source code Compiler (windows) Object code

Platform problem Source code Compiler (windows) Object code Compiler (linux) Compiler (mac) Source code Object code

java Java Source code Java Source code Compiler Intermediate code Phase I Intermediate code Interpreter Object code execution Object code execution Phase II

Byte code Interpreter (windows) Object code Interpreter (linux) Interpreter (mac) Byte code Object code

Byte code Windows Operating system Windows Operating system Linux Operating system Linux Operating system Mac Operating system Mac Operating system JVM Byte Code

Byte code Java Source code Java Source code Compiler Intermediate code Phase I Intermediate code Interpreter Object code execution Object code execution Phase II 0 iload_1 1 iload_2 2 iadd 3 istore_3 A=10 B=20 C=A+B

Java Features  Simple C++ Java Object oriented Polymorphism Inheritance.... multithreading

Java’s Features  Platform Independent

Java’s Features  Pure Object Oriented Class One { Public static void main(String arg[]) { System.out.println(“Hello World”); } Class One { Public static void main(String arg[]) { System.out.println(“Hello World”); } In java everything must be inside class.

Java’s Features  Strongly typed #include Void main() { printf(“Value of I”,&i); } Every variable used in program must be declared.

Java’s Features  Secure Internet When java bytecode come from network or another machine, in that case before executing that file verifier check first.

Java Features  Multithreading This is demo for checking multithreading concept of java. Developed in early c This is demo for checking multithreading concept of java. Spelling checking operation running while used giving input Developed in early java

Java’s Features  Architectural Neutral  Distributed  Garbage Collection

Garbage collection Memory One ob1=new One(); Two ob2=new Two(); two class object one class object Ob1Ob2 Ob1=ob2;

Java Package  Java package has two parts  JDK( java development Kit)  JRE(java runtime environment)  Java version 1.7

Hello program in c #include void main() { printf(“hello world”); } 1. For including header files. 2. main function start 3. print on screen hello world #include void main(int argc, char * argv[]) { printf(“hello world”); }

Java first program  Java is pure object oriented programming language. Class demo { } main funtion(arguments) { print statement for hello world }

First Java Program import java.lang.*; Class demo { public static void main(String arg[]) { System.out.println(“Hello World”); } For including libraries in java(like include statement in c) Name of class To print message on screen(like printf function) Main method in java. With string type of argument

Commands to run  Save file with name of class, which contains main method  javac demo.java  java demo

Creating, Compiling And Running Java Programs javac Java compiler Java byte code filename.class To compile the program at the command line type "javac filename.java" To run the interpreter, at the command line type "java filename" java Java Interpreter Type it in with the text editor of your choice filename.java (java file) Java program

Compiling The Smallest Java Program public class Smallest { public static void main (String[] args) { } Smallest.java javac (Java byte code) : : Smallest.class Type “javac Smallest.java”

Running The Smallest Java Program (Java byte code) : : Smallest.class java Type “java Smallest” (Platform/Operating specific binary : :

A Java program /* Here you describe what your program does. */ public class CLASS-NAME { public static void main (String args[]) { // your program goes here } // end of main } // end of class

Compiling and Running In order to run a Java program:  First you compile it  that is, you run a program called compiler that checks whether the program follows the Java syntax  if it finds errors, it lists them  If there are no errors, it translates the program into Java bytecode  Example: assume you created a program called Hello.java prompt>javac Hello.java  If successful, this creates a file Hello.class which contains the translation (Java bytecode) of Hello.java  Then you execute it  That is, you call the Java Virtual Machine to interpret and execute the Java bytecode of your program  Example: prompt>java Hello

Hello world program When learning a new language, the first program people usually write is one that salutes the world :). Here is the Hello world program in Java. /* This program prints out “hello world!” and terminates. */ public class Hello { public static void main (String args[]) { System.out.println(“Hello world!”); } // end of main } // end of class

Notes  Comments  what follows after // on the same line is considered comment  Or, what is in between /* this is a comment */  Indentation  is for the convenience of the reader; compiler ignores all spaces and new lines ; the delimiter for the compiler is the semicolon  All instructions end by semicolon  Lower vs. upper case matters!!  Void is different than void  Main is different that main

Writing to user (output) System.out.println(variable-name); prints the value of variable to the user System.out.println(“any message “); prints the message within quotes to the user System.out.println(“hello” + “world” + a + “plus“ + b); assuming the value of a is 3 and of b is 7, it prints helloworld3plus7 Note: System.out.println() always prints on a new line.

Example /* This program illustrates the System.out.println command. */ public class Hello { public static void main (String args[]) { System.out.println(“This is my first Java program!”); System.out.print(“I like Java.”); System.out.print(“I think Java is cool.”); } // end of main } // end of class  Exercise: change the print to println above. What is the difference?

Variable declaration type variable-name; Meaning: variable will be a variable of type Where type can be:  int//integer  double//real number Example: int a, b, c; double x; int sum;

Example /* Printing ages. */ public class MyFirstJavaProgram { public static void main (String args[]) { int myAge, myFriendAge; /* declare two integer variables */ myAge = 20; myFriendAge = myAge + 1; //one year older System.out.println(“Hello, I am “ +myAge + “years old, and my friend is “ + myFriendAge + “ years old”); System.out.println(“Goodbye”); } // end of main } // end of class

If statements if (condition) { //instructions } else { //instructions } //instructions condition instructions TrueFalse instructions

Boolean conditions..are built using  Comparison operators == equal != not equal < less than > greater than <= less than or equal >= greater than or equal  Example: int x, y; //two variables //assume they have some values if (x <= y) { System.out.println(“x is smaller”); } else { System.out.println(“x is larger”); }

Java data types TypeDescription byte8 bit signed integer short16 but signed integer int32 bit signed integer long64 bit signed integer float32 bit signed real number double64 bit signed real number char16 bit Unicode character (ASCII and beyond) boolean1 bit true or false value StringA sequence of characters between double quotes ( "" )

Primitive types Primitive typeSizeMinimumMaximum boolean1-bit —— char16-bitUnicode 0Unicode byte8-bit short16-bit int32-bit long64-bit float32-bitIEEE754 double64-bitIEEE754

Default values for primitive members  When a primitive type data is a member of a class, it’s guaranteed to get a default value even if you don’t initialize it.  Not true for those local variables!!  There will be compile error if you use it without initialization Primitive typeDefault booleanfalse char ‘ \u0000 ’ (null) byte(byte)0 short(short)0 int0 long0L float0.0f double0.0d

Example class Hello{ public static void main ( String[] args ) { int x; System.out.println(x); } >javac Hello.java Hello.java:5: variable x might not have been initialized System.out.println(x); ^ 1 error

Arrays in Java  An ordered collection of something, addressed by integer index  Something can be primitive values, objects, or even other arrays. But all the values in an array must be of the same type.  Only int or char as index  long values not allowed as array index  0 based  Value indexes for array “a” with length 10  a[0] – a[9];  a.length==10  Note: length is an attribute, not method

Arrays in Java: declaration  Declaration  int[] arr;  Person[] persons;  Also support: int arr[]; Person persons[]; (confusing, should be avoided)  Creation  int[] arr = new int[1024];  int [][] arr = { {1,2,3}, {4,5,6} };  Person[] persons = new Person[50];

Arrays in Java: safety  Cannot be accessed outside of its range  ArrayIndexOutOfBoundsException  Guaranteed to be initialized  Array of primitive type will be initialized to their default value  Zeroes the memory for the array  Array of objects: actually it’s creating an array of references, and each of them is initialized to null.

Arrays in Java:  second kind of reference types in Java int[] arr = new int [5]; arr int[][] arr = new int [2][5]; arr[0] arr[1] arr

Scoping  Scope determines both the visibility and lifetime of the names defined within the scope  Scope is determined by the placement of {}, which is called block. { int x = 10; //only x available { int y = 20; //both x and y available } //only x available, y out of scope! }

Scoping  Notice, you cannot do the following, although it’s legal in C/C++. { int x = 10; { int x = 20; } Compile error Hello.java:6: x is already defined in main(java.lang.String[]) int x =20; ^ 1 error

Scope of objects  When you create an object using new, the object hangs around past the end of the scope, although the reference vanishes. { String s = new String("abc"); } Reference s vanishes, but the String object still in memory Solution: Garbage Collector!

Importing library  If you need any routines that defined by java package import java.util.*; import java.io.*;  Put at the very beginning of the java file  java.lang.* already been imported.  Check javadoc for the classes

Static keyword  Want to have only one piece of storage for a data, regardless how many objects are created, or even no objects created  Need a method that isn’t associated with any particular object of this class  static keyword apply to both fields and methods  Can be called directly by class name  Example: java.lang.Math  Non-static fields/methods must be called through an instance

main() class Hello{ int num; public static void main(String[] args) { num = 10; } >javac Hello.java Hello.java:4: non-static variable num cannot be referenced from a static context num = 10; ^ 1 error

Main() doesn’t belong in a class  Always static  Because program need a place to start, before any object been created.  Poor design decision  If you need access non-static variable of class Hello, you need to create object Hello, even if main() is in class Hello! class Hello{ int num; public static void main(String[] args){ Hello h = new Hello(); h.num = 10; }