Java Basic.

Slides:



Advertisements
Similar presentations
Chapter 1: Computer Systems
Advertisements

Programming with Java. Problem Solving The purpose of writing a program is to solve a problem The general steps in problem solving are: –Understand the.
1 Chapter 2 Introduction to Java Applications Introduction Java application programming Display ____________________ Obtain information from the.
IntroductionIntroduction  Computer program: an ordered sequence of statements whose objective is to accomplish a task.  Programming: process of planning.
George Blank University Lecturer. CS 602 Java and the Web Object Oriented Software Development Using Java Chapter 4.
The Java Programming Language
Outline Java program structure Basic program elements
COMP 14: Intro. to Intro. to Programming May 23, 2000 Nick Vallidis.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
Java Software Solutions Lewis and Loftus Chapter 2 1 Copyright 1997 by John Lewis and William Loftus. All rights reserved. Software Concepts -- Introduction.
Prepared by Uzma Hashmi Instructor Information Uzma Hashmi Office: B# 7/ R# address: Group Addresses Post message:
University of Limerick1 Work with API’s. University of Limerick2 Learning OO programming u Learning a programming language can be broadly split into two.
1 Identifiers  Identifiers are the words a programmer uses in a program  An identifier can be made up of letters, digits, the underscore character (
LESSON 2 CREATING A JAVA APPLICATION JAVA PROGRAMMING Compiled By: Edwin O. Okech [Tutor, Amoud University]
Java: Chapter 1 Computer Systems Computer Programming II.
System development with Java Lecture 2. Rina Errors A program can have three types of errors: Syntax and semantic errors – called.
1 Computer Systems -- Introduction  Chapter 1 focuses on:  the structure of a Java application  basic program elements  preparing and executing a program.
Introduction to Programming David Goldschmidt, Ph.D. Computer Science The College of Saint Rose Java Fundamentals (Comments, Variables, etc.)
The Java Programming Language
CSC204 – Programming I Lecture 4 August 28, 2002.
Lecture 2 Object Oriented Programming Basics of Java Language MBY.
CS 11 java track: lecture 1 Administrivia need a CS cluster account cgi-bin/sysadmin/account_request.cgi need to know UNIX
Hello.java Program Output 1 public class Hello { 2 public static void main( String [] args ) 3 { 4 System.out.println( “Hello!" ); 5 } // end method main.
Chapter 2: Java Fundamentals
 Pearson Education, Inc. All rights reserved Introduction to Java Applications.
Page: 1 การโปรแกรมเชิงวัตถุด้วยภาษา JAVA บุรินทร์ รุจจนพันธุ์.. ปรับปรุง 15 มิถุนายน 2552 Keyword & Data Type มหาวิทยาลัยเนชั่น.
Chapter 1: Introduction Java Programming Language How the Java Virtual Machine Works (compiling, etc…) Update by: Dan Fleck Coming up: The Java Programming.
 Character set is a set of valid characters that a language can recognise.  A character represents any letter, digit or any other sign  Java uses the.
Java The Java programming language was created by Sun Microsystems, Inc. It was introduced in 1995 and it's popularity has grown quickly since A programming.
1 Programming Java Java Basics. 2 Java Program Java Application Program Application Program written in general programming language Applet Program running.
Console Input and Output JavaMethods An Introduction to Object-Oriented Programming Maria Litvin Gary Litvin Copyright © 2003 by Maria Litvin, Gary Litvin,
© 2011 Pearson Education, publishing as Addison-Wesley Chapter 1: Computer Systems Presentation slides for Java Software Solutions for AP* Computer Science.
Object Oriented Programming Lecture 2: BallWorld.
Introduction to Java Programming by Laurie Murphy Revised 09/08/2016.
SESSION 1 Introduction in Java. Objectives Introduce classes and objects Starting with Java Introduce JDK Writing a simple Java program Using comments.
 It is a pure oops language and a high level language.  It was developed at sun microsystems by James Gosling.
1 ENERGY 211 / CME 211 Lecture 3 September 26, 2008.
1 Problem Solving  The purpose of writing a program is to solve a problem  The general steps in problem solving are: Understand the problem Dissect the.
C++ Lesson 1.
Object Oriented Programming in
Lecture 2 D&D Chapter 2 & Intro to Eclipse IDE Date.
JAVA MULTIPLE CHOICE QUESTION.
Working with Java.
Chapter 4 Assignment Statement
Chapter No. : 1 Introduction to Java.
Objectives You should be able to describe: Interactive Keyboard Input
Yanal Alahmad Java Workshop Yanal Alahmad
Lecture 2: Data Types, Variables, Operators, and Expressions
Internet and Java Foundations, Programming and Practice
CS116 OBJECT ORIENTED PROGRAMMING II LECTURE 1 Part II
University of Central Florida COP 3330 Object Oriented Programming
Java package classes Java package classes.
Chapter 3 Assignment Statement
Unit 41 – Programing in Java
Statements, Comments & Simple Arithmetic
Starting JavaProgramming
Lecture 13 Input/Output Files.
null, true, and false are also reserved.
Introduction to Java Programming
An overview of Java, Data types and variables
Chapter 1: Computer Systems
Units with – James tedder
Units with – James tedder
JavaScript Reserved Words
Object Oriented Programming in java
Focus of the Course Object-Oriented Software Development
Module 2 - Part 1 Variables, Assignment, and Data Types
Introduction to Java Applications
Chap 2. Identifiers, Keywords, and Types
Computer Programming-1 CSC 111
Presentation transcript:

Java Basic

What is Java? Bytecode for virtual machines RMI, Servlet, Applet Every thing is a class "...A simple, pure object-oriented, distributed, interpreted, robust, secure, architecture neutral, portable, high-performance, multi-threaded, and dynamic language.“ [Sun Microsystems, Summer 95] Strong typing + no pointer + auto garbage collection Java is designed to reduce many security risks.

Write Once, Run Anywhere The Java Platform Application programs in bytecode App. Program 1 App. program 2 App. program 3 App. program 4 Java Virtual Machine (Bytecode interpreter) O. S. Windows Linux BSD Unix System V Unix Hardware Intel PowerPC SPARC Write Once, Run Anywhere

Lexical Structure of Java The lexical structure of a programming language is the set of elementary rules that define what are the tokens or basic atoms of the program. Some of the basic rules for Java are: Java is case sensitive. Whitespace, tabs, and newline characters are ignored except when part of string constants. They can be added as needed for readability. Single line comments begin with // Multiline comments begin with /* and end with */ Documentary comments begin with /** and end with **/ Simple statements terminate in semicolons! Make sure to always terminate simple statements with a semicolon.

Reserved Words Reserved words are words that can't be used as identifiers. Many of them are keywords that have a special purpose in Java. Java reserved keywords: (45) abstract, boolean, break, byte, case, catch, char, class, continue, default, do, double, else, extends, final, finally, float, for, if, implements, import, instanceof, int, interface, long, native, new, package, private, protected, public, return, short, static, super, switch, synchronized, this, throw, throws, transient, try, void, volatile, while. const, goto are reserved keywords not in use. null, true, false are reserved literals in java.  

Java Types Primitive types byte (8 bits), short (16 bits), int (32 bits), long (64 bits) float (32 bits), double (64 bits) char - unicode! e,g, ‘\u12ab’ (16 bits) boolean (16 bits, true/false) Reference types (Subtypes of Object) Classes: String etc. Arrays Note: The default values for variables are 0/false/null. Operators +, -, *, /, <<, >>, >>>, &, |, ^, &&, ||, ... etc.

A simple Java program // Hello.java public class Hello { public static void main(String[] args) { System.out.println("Hello, World!"); }

main() method java interpreter starts by calling the public class's main() method. main() method takes an array of String as an argument main() method must be declared public, static and not return a value (void). Signature of the main() method can be any of these: public static void main(String args[] ) public static void main (String [] args ) static public void main (String [] args) note: args can instead be any valid identifier like "anything"

Online resources – The official Java site : http://www. oracle The Java SE Development Kit (JDK) classes.zip (This's needed by the compiler & interpreter.) src.zip Java Compiler (javac.exe) Java Interpreters (java.exe jre.exe) Java Applet Viewer (appletviewer.exe) Java Debugger (jdb.exe) Java Documentation Generator (javadoc.exe) etc.

To compile and run a Java program using JDK C:\> javac Hello.java (The compiler creates Hello.class) C:\> java Hello (The interpreter excutes Hello.class.) input output Hello.Java Javac Hello.class Java Java Compiler Java bytecode Java Virtual machine Program in Java

To compile and run a Java program on athena [wang@athena:12] javac Hello.java [wang@athena:13] java Hello Or use ahead-of-time compiler for Java, gcj, and GNU interpreter for Java bytecode, gij. [wang@athena:14] gcj Hello.java --main=Hello [wang@athena:15] a.out or [wang@athena:16] gcj –C Hello.java [wang@athena:17] gij Hello -C means  Hello.class

To compile a Java program on line There are web sites providing remote compiling services, e.g. http://www.innovation.ch/java/java_compile.html

Input from the Java Console Scanner class provides methods for reading byte, short, int, long, float, double, and String data types from the Java console or text files. Scanner is in the java.util package. Scanner parses (separates) input into sequences of characters called tokens. By default, tokens are separated by standard white space characters (tab, space, newline, etc.) Java 1.5 or later only.

A Scanner Constructor Scanner( InputStream source ) Example: Scanner scan = new Scanner( System.in ); Scanner( InputStream source ) creates a Scanner object for reading from source. If source is System.in, this instantiates a Scanner object for reading from the Java console

Scanner next… Methods Return type Method name and argument list dataType nextDataType( ) returns the next token in the input stream as a dataType. dataType can be byte, int, short, long, float, double, or boolean String next( ) returns the next token in the input stream as a String nextLine( ) returns the remainder of the line as a String

Example import java.util.Scanner; public class scannerTest { public static void main(String[] args) { Scanner stdin = new Scanner(System.in); do { if (stdin.hasNextInt()) { int n = stdin.nextInt(); System.out.println("Integer entered: "+n); } else if (stdin.hasNext("[a-zA-Z]*")) { String s = stdin.next(); System.out.println("String entered: "+s); } else break; } while (true); }

Output to the Java Console System.out is the default standard output device, which is tied to the Java Console. 3 commonly used methods: System.out.print("Hello!"); System.out.println("Hello!"); System.out.printf("%10s","Hello"); Displays formatted data.

printf() printf (String format, Object... args) The "..." indicates the varargs functionality (also referred to as variable arity methods). Convert values arg1, earg2, etc. to characters according to conversion specifiers. A conversion specifier begins with the character % and ends with a conversion character.

printf() example int x = 12; float y = 3.5; System.out.printf("x = %d and y = %f\n", x, y); Conversion specifiers Output: x = 12 and y = 3.5

Conversion Characters value type Converted to b, B boolean “True”/”false” c, C character Single character s, S String Character strings d int Signed decimal integer o Unsigned octal integer x,X Unsigned hexadecimal f double d.dddddd e,E d.dddddE+xx g,G In f or e style whichever is smaller h, H general Hex string

Field Width and Precision Put between % and conversion character Field width specifies minimum number of columns to be used. If the field width is positive, right justification is used. If the field width is negative, left justification is used. Period to separate field width from precision Precision specifies String’s maximum number of characters to be printed. Number of digits after the decimal point

Flags Put between % and the conversion character Plus sign (+), to display plus or negative sign for numbers. Space, put a space before positive number. Pound sign (#), to put 0 for octal, 0x for hexadecimal, force a decimal point for floats. Zero (0), to pad a field with leading zeros

Example Output: aloha 5 Z 5.670000 aloha 5 Z 5.670000 alo 5.670 public class printfTest1 { public static void main(String[] args) { System.out.printf("%9s%9d%9c%9f\n","aloha",5,'Z',5.67); System.out.printf("%-9s%-9d%-9c%9f\n","aloha",5,'Z',5.67); System.out.printf("%-9.3s%-9.3f\n","aloha",5.67); System.out.printf("%x, %#x\n", 123, 123); System.out.printf("%09d\n", 123); } Output: aloha 5 Z 5.670000 aloha 5 Z 5.670000 alo 5.670 7b, 0x7b 000000123

Argument index Format: n$ Example: Output: 1 1 3 2 public class printfTest { public static void main(String[] args) { System.out.printf("%1$d %1$d %3$d %2$d\n",1,2,3,4,5); } Output: 1 1 3 2

Libraries Java programs are usually not written from scratch. There are hundreds of library classes for all occasions. Library classes are organized into packages. For example: java.util — miscellaneous utility classes java.awt — window and graphics toolkit javax.swing — GUI development package

Import library classes A fully-qualified library class name includes the package name. For example: java.awt.Color javax.swing.JButton Import statements at the top of the source file let you refer to library classes by their short names. import javax.swing.JButton; JButton go = new JButton("Go"); package class Fully-qualified name short name

Import library classes conti. You can import all classes of a package by using a wildcard .*: import java.awt.*; import java.awt.event.*; import javax.swing.*; java.lang is imported automatically into all classes; defines System, Math, Object, String, and other commonly used classes. Imports all classes from awt, awt.event, and swing packages

Homework. Show lines printed. System.out.printf("%d%10d\n", 123, 123); System.out.printf("%‑10d%x\n", 123, 123); column 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 Line 1 Line 2 Line 3 Line 4