Apr, 2011 Dating with Java Larry Li. Objective Hello world program Setup development environment Data types and variables Operators and Expressions Control.

Slides:



Advertisements
Similar presentations
AP Computer Science Anthony Keen. Computer 101 What happens when you turn a computer on? –BIOS tries to start a system loader –A system loader tries to.
Advertisements

Air Force Institute of Technology Electrical and Computer Engineering
L2:CSC © Dr. Basheer M. Nasef Lecture #2 By Dr. Basheer M. Nasef.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 2 1 Chapter 2 Primitive.
Data Types in Java Data is the information that a program has to work with. Data is of different types. The type of a piece of data tells Java what can.
Primitive Data Types and Operations. Introducing Programming with an Example public class ComputeArea { /** Main method */ public static void main(String[]
Bellevue University CIS 205: Introduction to Programming Using C++ Lecture 3: Primitive Data Types.
Shlomo Hershkop1 Introduction to java Class 1 Fall 2003 Shlomo Hershkop.
1 Chapter 4 Language Fundamentals. 2 Identifiers Program parts such as packages, classes, and class members have names, which are formally known as identifiers.
Lecture 3 Java Basics Lecture3.ppt.
Review… Yong Choi BPA CSUB. Access Modifier public class HelloWorldApp –More detailes of Java key (reserved) wordJava key (reserved) word –The keyword,
Java Syntax Primitive data types Operators Control statements.
Introduction to Computers and Programming Lecture 5 Boolean type; if statement Professor: Evan Korth New York University.
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie June 27, 2005.
Hello, world! Dissect HelloWorld.java Compile it Run it.
1 Chapter Two Using Data. 2 Objectives Learn about variable types and how to declare variables Learn how to display variable values Learn about the integral.
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved. 1 Chapter 2 Elementary Programming.
Java Fundamentals Expanded SE1021 Dr. Mark L. Hornick 1.
Java Primitives The Smallest Building Blocks of the Language (corresponds with Chapter 2)
DAT602 Database Application Development Lecture 5 JAVA Review.
Primitive Data Types and Operations Identifiers, Variables, and Constants Primitive Data Types Byte, short, int, long, float, double, char, boolean Casting.
General Features of Java Programming Language Variables and Data Types Operators Expressions Control Flow Statements.
By Nicholas Policelli An Introduction to Java. Basic Program Structure public class ClassName { public static void main(String[] args) { program statements.
1 Do you have a CS account? Primitive types –“ building blocks ” for more complicated types Java is strongly typed –All variables in a Java program must.
Introduction to Programming David Goldschmidt, Ph.D. Computer Science The College of Saint Rose Java Fundamentals (Comments, Variables, etc.)
Basic Java Programming CSCI 392 Week Two. Stuff that is the same as C++ for loops and while loops for (int i=0; i
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.
C STRUCTURES. A FIRST C PROGRAM  #include  void main ( void )  { float height, width, area, wood_length ;  scanf ( "%f", &height ) ;  scanf ( "%f",
Java Programming: From Problem Analysis to Program Design, 4e Chapter 2 Basic Elements of Java.
An Introduction to Java – Part 1 Dylan Boltz. What is Java?  An object-oriented programming language  Developed and released by Sun in 1995  Designed.
Java development environment and Review of Java. Eclipse TM Intergrated Development Environment (IDE) Running Eclipse: Warning: Never check the “Use this.
 The if statement and the switch statement are types of conditional/decision controls that allow your program.  Java also provides three different looping.
1 Operations Making Things Happen (Chap. 3) Expressions.
Primitive data Week 3. Lecture outcomes Primitive data – integer – double – string – char – Float – Long – boolean Declaration Initialisation Assignments.
Basic Java Syntax COMP 401, Spring 2014 Lecture 2 1/14/2014.
Java Language Basics By Keywords Keywords of Java are given below – abstract continue for new switch assert *** default goto * package.
Java Programming, Second Edition Chapter Two Using Data Within a Program.
A Simple Java Program //This program prints Welcome to Java! public class Welcome { public static void main(String[] args) { public static void main(String[]
 In the java programming language, a keyword is one of 50 reserved words which have a predefined meaning in the language; because of this,
Developed at Sun Microsystems in 1991 James Gosling, initially named “OAK” Formally announced java in 1995 Object oriented and cant write procedural.
1 Lecture 5 More Programming Constructs Instructors: Fu-Chiung Cheng ( 鄭福炯 ) Associate Professor Computer Science & Engineering Tatung Institute of Technology.
CSM-Java Programming-I Spring,2005 Fundamental Data Types Lesson - 2.
Java Part I By Wen Fei, HAO. Program Structure public class ClassName { public static void main(String[] args) { program statements } user defined methods.
2: Basics Basics Programming C# © 2003 DevelopMentor, Inc. 12/1/2003.
An Introduction to Java – Part 1 Erin Hamalainen CS 265 Sec 001 October 20, 2010.
Chapter 1 Java Programming Review. Introduction Java is platform-independent, meaning that you can write a program once and run it anywhere. Java programs.
0 Chap.2. Types, Operators, and Expressions 2.1Variable Names 2.2Data Types and Sizes 2.3Constants 2.4Declarations 2.5Arithmetic Operators 2.6Relational.
Session 2 Operators, Decisions and Loops. Objectives Operators Casting data Decision marking structures Loops break, continue, return.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Elementary Programming.
1 1 Chapter 2 Elementary Programming. 2 2 Motivations In the preceding chapter, you learned how to create, compile, and run a Java program. Starting from.
Java Basics. Tokens: 1.Keywords int test12 = 10, i; int TEst12 = 20; Int keyword is used to declare integer variables All Key words are lower case java.
Programming Principles Operators and Expressions.
JAVA Programming (Session 2) “When you are willing to make sacrifices for a great cause, you will never be alone.” Instructor: รัฐภูมิ เถื่อนถนอม
Primitive Data Types and Operations F Introduce Programming with an Example F Identifiers, Variables, and Constants F Primitive Data Types –byte, short,
Lecture 3: More Java Basics Michael Hsu CSULA. Recall From Lecture Two  Write a basic program in Java  The process of writing, compiling, and running.
CIS3931 – Intro to JAVA Lecture Note Set 2 17-May-05.
Programming in Java Sachin Malhotra, Chairperson, PGDM-IT, IMS Ghaziabad Saurabh Chaudhary, Dean, Academics, IMS Ghaziabad.
Sachin Malhotra Saurabh Choudhary
Lecture 2: Data Types, Variables, Operators, and Expressions
Java Programming: From Problem Analysis to Program Design, 4e
Chapter 2.
Starting JavaProgramming
مساق: خوارزميات ومبادئ البرمجة الفصل الدراسي الثاني 2016/2015
An Introduction to Java – Part I, language basics
Module 2: Understanding C# Language Fundamentals
Java Tokens & Data types
elementary programming
2. Second Step for Learning C++ Programming • Data Type • Char • Float
Chapter 2 Primitive Data Types and Operations
Introduction to java Part I By Shenglan Zhang.
Presentation transcript:

Apr, 2011 Dating with Java Larry Li

Objective Hello world program Setup development environment Data types and variables Operators and Expressions Control Flow Method

Java Tech Structure Java code will compile to byte code JVM will run byte code Write Once, Run Anywhere

Setup Development Environment Download JDK Set environment variables(JAVA_HOME, CLASSPATH, PATH) JAVA_HOME : “c:\Sun\jdk1.6.0_24” PATH : “%JAVA_HOME%\bin” CLASS_PATH:”.;%JAVA_HOME\lib\dt.jar”;%JAVA_HOME\lib\tools.jar” JAVA_HOME : “c:\Sun\jdk1.6.0_24” PATH : “%JAVA_HOME%\bin” CLASS_PATH:”.;%JAVA_HOME\lib\dt.jar”;%JAVA_HOME\lib\tools.jar”

public class Hello{ public static void main(String[] args){ System.out.println(“Hello World”); } public class Hello{ public static void main(String[] args){ System.out.println(“Hello World”); } Our First Program

Compile and Run Java source file name is “ClassName.java” Use javac to compile java program (c:\javac Hello.java) Use java to run java program(c:\java Hello)

Datatypes TypeSizeMinimumMaximumWrapper typeDefault boolean---Booleanfalse char16bits\u0000\uffffCharacter\u0000 byte8bits Byte0 short16bits Short0 int32bits Integer0 long64bits Long0 float32bitsIEEE754 Float0.0F double64bitsIEEE754 Double0.0D void---Void- reference----null

Declare Variables int age = 25; Declaration must before the first statement that uses the local variable. Must initialize a local variable before using it. final int height = 179; Variables declared with modifier final must be assigned before use and cannot be changed once assigned

Legal Identifier A variable name is a sequence of letters, digits, and underscores. Must start with a letter or underscore. Can be as long(or short)as you want. Must not be a keyword or a literal value

Java Keywords abstractcontinuefornewswitch assertdefaultgoto*packagesynchronized booleandoifprivatethis breakdoubleimplementsprotectedthrow byteelseimportpublicthrows caseenuminstanceofreturntransient catchextendsintshorttry charfinalinterfacestaticvoid classfinallylongstrictfpvolatile const*floatnativesuperwhile

Literals Boolean literals : ”true”, ”false”. Null literals : ”null”. Numeric literals : “10”,”15L”,”1.1F”,”2.0”,”6.5E+1f”,”2.65e-8” Character literals : ‘A’,’\u0041’. String literal : “James Gosling”.

Arrays A variable declared with brackets,[],is an array reference. Must use new to actually create the array, array is object. float price[]; String[] title, author; price = new new float[44]; String[] names = new String[10]; names[1] = “James Gosling”; price[0] = 99.95F; int odds = {1,3,5,7,9}; float price[]; String[] title, author; price = new new float[44]; String[] names = new String[10]; names[1] = “James Gosling”; price[0] = 99.95F; int odds = {1,3,5,7,9};

Operators Precedence !, ~, ++, --, +(positive), –(negative),( ) (cast) *,/,% +,-,>=,instanceof ==,!= && || Condition ? X : Y =,+= …

Operators Assignment Mathematical operators Increment and decrement Relational operators Logical operators Conditional operator Implicit type conversions Casting operators

Assignment Assign a value to a variable. Left operand must be a variable. Assignment expression's value is the value that was assigned Evaluated right to left min = 5; 5 = x;// illegal! current = (min = 5); max = current = min = 5; min = 5; 5 = x;// illegal! current = (min = 5); max = current = min = 5;

Mathematical +,-,*,/,% +,-(unary operator) age+1; daysLeft -3; width * height; totalLines / numPages; curLine%6; -balance age+1; daysLeft -3; width * height; totalLines / numPages; curLine%6; -balance

Increment and decrement ++,-- Unary operator Operand must be a variable --age; weight--; salary++; ++numCloth; --age; weight--; salary++; ++numCloth;

Relational >,>=,<,<=,==,!=. Results a true or false value. x >= maxValue args.length != 2 x >= maxValue args.length != 2

Logical ||,&&,! |,& && and || operators use short-circuit evaluation. length 18 height >= 10 && height <= 20 !(height >= 10 && height <=20) length 18 height >= 10 && height <= 20 !(height >= 10 && height <=20)

Operate-Assign Operators +=,-=,etc. Correspond to all the basic arithmetic operators age -= 10; mySalary += hisSalary; age -= 10; mySalary += hisSalary;

Conditional Operator op1 ? op2 : op3 char status; int age = 16; Status = age >= 18 ? ‘a’ : ‘m’ char status; int age = 16; Status = age >= 18 ? ‘a’ : ‘m’

Implicit Type Conversions Operands of different data types in an expression are converted to a common type. Smaller data types are converted to larger data types. Conversions that would truncate data will not happen implicitly float fTotal; float f = 6.5F; int i = 5,iTotal; fTotal = f + i; iTotal = f + i;// ERROR float fTotal; float f = 6.5F; int i = 5,iTotal; fTotal = f + i; iTotal = f + i;// ERROR

Cast Operator Conversions can be forced by explicitly casting a value or expression to a new data type; If the value being cast is too large to fit in the smaller location, the bits of the value will be truncated. To convert to a String, do not use the cast operator. float f = 6.5F, g = 7.7F; int total = (int)(f + g); String s = “” + f;// if one operand is String float f = 6.5F, g = 7.7F; int total = (int)(f + g); String s = “” + f;// if one operand is String

Control Flow Statement Conditional Statements (if-else, switch) Loop(do-while, while, for) Continue, Break statement Label in loop

Statement A single statement is an expression followed by a semicolon. Curly braces group statements into compound statements, called blocks. int area; { area = width * length; System.out.println(“area is ” + area); } int area; { area = width * length; System.out.println(“area is ” + area); }

Conditional (if) Statement if, if-else if, else if (age 3){ //do stuff } else if ( age >= 16 ){ //do stuff } else{ //else do stuff } if (age 3){ //do stuff } else if ( age >= 16 ){ //do stuff } else{ //else do stuff }

Conditional (switch) Statement Use a switch statement when you are testing the same expression for several possible literal values. Only compare expression of type byte, short, int, char, enum. switch (choice){ case ‘a’: // do something break; case ‘b’ : // do something break; default : // do default break; } switch (choice){ case ‘a’: // do something break; case ‘b’ : // do something break; default : // do default break; }

Loops do-while, while, for do{ // do something }while (hasNext); while(hasNext){ // do something } for(int i=0; i<5 ;i++){ // do something } int odds[] = {1,3,5,7,9}; for(int n : odds){ System.out.println(n); } do{ // do something }while (hasNext); while(hasNext){ // do something } for(int i=0; i<5 ;i++){ // do something } int odds[] = {1,3,5,7,9}; for(int n : odds){ System.out.println(n); }

Continue/break statement int i=0; while(true){ if (i % 2 != 0){ cotinue; } System.out.println(i); if (i > 10){ break; } int i=0; while(true){ if (i % 2 != 0){ cotinue; } System.out.println(i); if (i > 10){ break; }

Continue/break statement int i=0; while(true){ if (i % 2 != 0){ cotinue; } System.out.println(i); if (i > 10){ break; } int i=0; while(true){ if (i % 2 != 0){ cotinue; } System.out.println(i); if (i > 10){ break; }

Label in Loop label: while(true){ break; continue; continue label; break label; } label: while(true){ break; continue; continue label; break label; }

Methods //defing float getRectangleArea(float width,float height){ float area = width * height; return area; } //calling int rectangleArea = getRectangleArea(thisWidth,thisHeight); //defing float getRectangleArea(float width,float height){ float area = width * height; return area; } //calling int rectangleArea = getRectangleArea(thisWidth,thisHeight); Calling methods Defining methods Method Parameters

Finished Today Q&A

Homework 1.Setup development environment 2.A program print your name 3.Compile and run this program 4.A program print your dog’s information, including : name,weight,length of tail, friendly or not 5.Write a program with a big integer, big = and a bigger, bigger = big+1,print the tow number and explain the result 6.A program can convert Fahrenheit to Celsius (C = F minus 32 multiply by five ninth)

Thank you Java Bench