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.

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.
Primitive Data Types There are a number of common objects we encounter and are treated specially by almost any programming language These are called basic.
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.
IntroductionIntroduction  Computer program: an ordered sequence of statements whose objective is to accomplish a task.  Programming: process of planning.
The Java Programming Language
1 Chapter 4 Language Fundamentals. 2 Identifiers Program parts such as packages, classes, and class members have names, which are formally known as identifiers.
Review… Yong Choi BPA CSUB. Access Modifier public class HelloWorldApp –More detailes of Java key (reserved) wordJava key (reserved) word –The keyword,
Outline Java program structure Basic program elements
Loops – While, Do, For Repetition Statements Introduction to Arrays
Java Programming: From Problem Analysis to Program Design, 4e Chapter 7 User-Defined Methods.
CSci 142 Data and Expressions. 2  Topics  Strings  Primitive data types  Using variables and constants  Expressions and operator precedence  Data.
String Escape Sequences
Java Software Solutions Lewis and Loftus Chapter 2 1 Copyright 1997 by John Lewis and William Loftus. All rights reserved. Software Concepts -- Introduction.
1 Variables, Constants, and Data Types Primitive Data Types Variables, Initialization, and Assignment Constants Characters Strings Reading for this class:
1 Identifiers  Identifiers are the words a programmer uses in a program  An identifier can be made up of letters, digits, the underscore character (
Chapter 2: Basic Elements of Java J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design, Second Edition.
General Features of Java Programming Language Variables and Data Types Operators Expressions Control Flow Statements.
Introduction to Programming David Goldschmidt, Ph.D. Computer Science The College of Saint Rose Java Fundamentals (Comments, Variables, etc.)
The Java Programming Language
Lecture 2 Object Oriented Programming Basics of Java Language MBY.
C Derived Languages C is the base upon which many build C++ conventions also influence others *SmallTalk is where most OOP comes Java and Javascript have.
Outline Character Strings Variables and Assignment Primitive Data Types Expressions Data Conversion Interactive Programs Graphics Applets Drawing Shapes.
Chapter 2: Java Fundamentals
Java Programming: From Problem Analysis to Program Design, 4e Chapter 2 Basic Elements of Java.
Page: 1 การโปรแกรมเชิงวัตถุด้วยภาษา JAVA บุรินทร์ รุจจนพันธุ์.. ปรับปรุง 15 มิถุนายน 2552 Keyword & Data Type มหาวิทยาลัยเนชั่น.
CHAPTER 4 GC 101 Data types. DATA TYPES  For all data, assign a name (identifier) and a data type  Data type tells compiler:  How much memory to allocate.
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.
SE-1010 Dr. Mark L. Hornick 1 Variables & Datatypes.
© 2004 Pearson Addison-Wesley. All rights reserved ComS 207: Programming I Instructor: Alexander Stoytchev
Computing with C# and the.NET Framework Chapter 2 C# Programming Basics ©2003, 2011 Art Gittleman.
White Space Spaces, blank lines, and tabs are collectively called white space and are used to separate words and symbols in a program Extra white space.
Java Language Basics By Keywords Keywords of Java are given below – abstract continue for new switch assert *** default goto * package.
Programming in Java (COP 2250) Lecture 4 Chengyong Yang Fall, 2005.
Operators and Expressions. 2 String Concatenation  The plus operator (+) is also used for arithmetic addition  The function that the + operator performs.
General Features of the Java Programming Language Variables and Data Types Operators Expressions Control Flow Statements.
User Defined Methods Methods are used to divide complicated programs into manageable pieces. There are predefined methods (methods that are already provided.
 In the java programming language, a keyword is one of 50 reserved words which have a predefined meaning in the language; because of this,
Quick Review of OOP Constructs Classes:  Data types for structured data and behavior  fields and methods Objects:  Variables whose data type is a class.
2: Basics Basics Programming C# © 2003 DevelopMentor, Inc. 12/1/2003.
Chapter 1 Java Programming Review. Introduction Java is platform-independent, meaning that you can write a program once and run it anywhere. Java programs.
Java Programming: From Problem Analysis to Program Design, Second Edition 1 Lecture 1 Objectives  Become familiar with the basic components of a Java.
Session 2 Operators, Decisions and Loops. Objectives Operators Casting data Decision marking structures Loops break, continue, return.
© 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.
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.
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.
© 2004 Pearson Addison-Wesley. All rights reserved August 27, 2007 Primitive Data Types ComS 207: Programming I (in Java) Iowa State University, FALL 2007.
Key Words / Reserved Words
Working with Java.
Chapter 4 Assignment Statement
Primitive Data Types August 28, 2006 ComS 207: Programming I (in Java)
Lecture 2: Data Types, Variables, Operators, and Expressions
Multiple variables can be created in one declaration
Variables and Arithmetic Operators in JavaScript
University of Central Florida COP 3330 Object Oriented Programming
Chapter 3 Assignment Statement
Computing with C# and the .NET Framework
Java Programming: From Problem Analysis to Program Design, 4e
Starting JavaProgramming
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
Focus of the Course Object-Oriented Software Development
Chap 2. Identifiers, Keywords, and Types
Agenda Types and identifiers Practice Assignment Keywords in Java
Presentation transcript:

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 6 } // end class Hello Hello! A Simple Program: Printing a Line of Text Chapter 0 – Introduction to Programming

2 Data Types

3 Identifiers Identifiers are names for programming entities such as variables, classes, methods, etc. Rules for naming identifiers: 1.consists of letters (a-z, A-Z), digits (0-9), underscore (_)and dollar sign ($) 2.cannot start with a digit 3.cannot be reserved words (see next page) Example of Valid Identifiers: –a, aNumber, F101, Sales_2003$123, MyClass Example of Invalid Identifiers: –Go#, 3Hands, Star*Mars, final

4 Reserved Words abstract default if private this boolean do implements protected throw break double import public throws byte else instanceof return transient case extends int short try catch final interface static void char finally long strictfp volatile class float native super while const for new switch continue goto package synchronized true false null

5 Constants A constant is an identifier that is similar to a variable except that its value cannot be changed. The compiler will issue an error if you try to change a constant In Java, we use the final modifier to declare a constant final int MEANING_OF_LIFE = 42; Constants: –give names to otherwise unclear literal values –facilitate changes to the code Avoid using literal constants (e.g. salary = 2*hourly_rate + 60; ) should use salary = OT_FACTOR*hourly_rate + BASIC_RATE;

6 Assignment An assignment statement changes the value of a variable The assignment operator is the = sign You can only assign a value to a variable that is consistent with the variable's declared type The expression on the right is evaluated and the result is stored in the variable on the left The value that was in total is overwritten total = 55*salary;

7 Arithmetic Operators

8

9

10 String Concatenation The plus operator (+) is used for arithmetic addition and string concatenation (joining two strings) The function to perform depends on the type of the information e.g gives 37 but "2" + "35" gives "235" How about 2 + "35" or "2" + 35? Ans: "235" If both operands are strings, or if one is a string and one is a number, it performs string concatenation If both operands are numeric, it adds them

11 Casting Casting - convert a type to another explicitly To cast, the type is put in parentheses in front of the value being converted For example, if total and count are integers, but we want a floating point result when dividing them, we can cast total : result = (float) total / count; To avoid loss of accuracy, use a = (double) 10/3; a = 10/(double) 3; but NOT a = (double) (10/3); Note: a=10/3.0 or a=10.0/3 also work

12 Equality and Relational Operators e.g. a=2; b=4; if (a>b) System.out.print("hot - "); System.out.prinln("but not humid"); The output is but not humid.

13 Logical Operators Logical operators –Allows for forming more complex conditions –Combines simple conditions Java logical operators –&& (logical AND) –& (boolean logical AND) –|| (logical OR) –| (boolean logical inclusive OR) –^ (boolean logical exclusive OR) –! (logical NOT)

14 The if/else Selection Structure Perform action ( statement 1 ) only when condition is true Perform different specified action ( statement 2 ) when condition is false if ( condition ) statement 1 else statement 2

15 Conditional Operator The conditional operator is similar to an if-else statement, except that it is an expression that returns a value. For example: if (num1 > num2) larger = num1; else larger = num2; is the same as larger = (num1 > num2) ? num1 : num2;

16 Nested if statements expression 1 statement 3 expression 2 statement 2 Statement 1 if (expression 1) if (expression 2) statement 1 else statement 2 else statement 3 true false

17 The switch Multiple-Selection Structure switch structure –Used for multiple selections Syntax expression must be byte, short, int or char but NOT long switch ( expression ) { case label 1 : statement(s); break; case label 2 : statement(s); break; default: statement(s); }

18 The for Repetition Structure for ( expression1; expression2; expression3 ) { statement; } can easily be rewritten as: expression1; while ( expression2 ) { statement; expression3; }

19 The while Repetition Structure The structure is: while ( condition ) { statement 1 ; statement 2 ; statement 3 ;... } Loop Body

20 The do/while Repetition Structure do/while structure –Similar to while structure –Tests loop-continuation after performing body of loop i.e., loop body always executes at least once Syntax do { statements;... } while ( condition );

21 Nested Loop A loop inside the loop body of another loop. Example: Print the following pattern using nested for-loop. public class NestedLoop1 { public static void main( String args[] ) { for ( int i = 1; i <= 7; i++ ) { // print each row for ( int j = 1; j <= 5; j++ ) { System.out.print( j ); } System.out.println(); } Outer Loop Inner Loop 12345

22 Arrays Consider the declaration int[] c = new int[ 5 ]; -c is the array name –c.length accesses array c ’s length (5) –c has 5 elements ( c[0], c[1], … c[4] ) –c[1] = 20 assigns integer 20 to the second location of c An array of size N is indexed from zero to N-1 We can use initializer list to initialize array elements as int[] n = { 10, 20, 30, 40, 50 }; The operator new is not needed.

23 Arrays Declaring and Allocating arrays –Arrays are objects that occupy memory –Allocated dynamically with operator new int[] c = new int[ 12 ]; –Equivalent to int[] c; // declare array c = new int[ 12 ]; // allocate array We can allocate arrays of objects too String[] b = new String[ 100 ]; Bear in mind –All primitive data type variables are allocated automatically. –All object data type variables (including array) must be created by the programmer (using the new operator) explicitly.

24 Arrays Multiple-subscripted arrays –Tables with rows and columns Double-subscripted (two-dimensional) array Declaring double-subscripted array b[2][2] int b[][] = { { 1, 2 }, { 3, 4 } }; –1 and 2 initialize b[0][0] and b[0][1] –3 and 4 initialize b[1][0] and b[1][1]

25 Arrays Allocating multiple-subscripted arrays –Can be allocated dynamically 3 -by- 4 array int b[][]; b = new int[ 3 ][ 4 ]; Rows can have different number of columns int c[][]; c = new int[ 2 ][ ]; // allocate rows c[ 0 ] = new int[ 5 ]; // allocate row 0 c[ 1 ] = new int[ 3 ]; // allocate row 1 b c

26 a[ 0 ][ 0 ] a[ 1 ][ 0 ] a[ 2 ][ 0 ] a[ 0 ][ 1 ] a[ 1 ][ 1 ] a[ 2 ][ 1 ] a[ 0 ][ 2 ] a[ 1 ][ 2 ] a[ 2 ][ 2 ] a[ 0 ][ 3 ] a[ 1 ][ 3 ] a[ 2 ][ 3 ] Row 0 Row 2 Row 1 Column 0Column 1Column 2Column 3 Column subscript (or index) Row Subscript (or index) Array name Fig A double-subscripted array with three rows and four columns. Arrays

27 Method Definitions General format of a method: return-value-type method-name( parameter-list ) { declarations and statements } Method can also return values: return expression ; Parameters must be correct –When a method starts running, it must have the right number of parameters, and each parameter must be of the required type. public int square( int y ) { return y * y; } Access Modifier (discussed later)

28 – Specify how to convert types without data loss Promotion Rules

29 Scope Rules A method can see local variables and parameters that are within it’s scope (inside the box). A method can see out of the glass that surrounds it. But no outsider can see into the box.

30 Method Overloading Method overloading –Several methods of the same name –Different parameter set for each method Number of parameters Parameter types –Return type cannot be used to distinguish overloaded methods. Examples from Java Library –The class Arrays has many overloaded methods