Huazhong University of Science and Technology JAVA Programming Language Lecture 2:Variables and Data Types

Similar documents
Introduction to Java Applications Pearson Education, Inc. All rights reserved.

Lecture 1 Introduction to Java

Pemrograman Dasar. Basic Elements Of Java

Introduction to Java

Topics. Parts of a Java Program. Topics (2) CS 146. Introduction To Computers And Java Chapter Objectives To understand:

CS 106 Introduction to Computer Science I

Variables, Constants, and Data Types

Chapter 2: Elements of Java

Introduction to Java. CS 3: Computer Programming in Java

Handout 1. Introduction to Java programming language. Java primitive types and operations. Reading keyboard Input using class Scanner.

JAVA - QUICK GUIDE. Java SE is freely available from the link Download Java. So you download a version based on your operating system.

Java Crash Course Part I

Moving from CS 61A Scheme to CS 61B Java

Introduction to Visual C++.NET Programming. Using.NET Environment

Name: Class: Date: 9. The compiler ignores all comments they are there strictly for the convenience of anyone reading the program.

First Java Programs. V. Paúl Pauca. CSC 111D Fall, Department of Computer Science Wake Forest University. Introduction to Computer Science

Chapter One Introduction to Programming

C++ Language Tutorial

CSE 1223: Introduction to Computer Programming in Java Chapter 2 Java Fundamentals

JavaScript: Introduction to Scripting Pearson Education, Inc. All rights reserved.

Bachelors of Computer Application Programming Principle & Algorithm (BCA-S102T)

JavaScript: Control Statements I

Java CPD (I) Frans Coenen Department of Computer Science

Informatica e Sistemi in Tempo Reale

Install Java Development Kit (JDK) 1.8

Chapter 3. Input and output. 3.1 The System class

Lecture 5: Java Fundamentals III

Number Representation

Java Basics: Data Types, Variables, and Loops

Java Interview Questions and Answers

2 ASCII TABLE (DOS) 3 ASCII TABLE (Window)

C++ INTERVIEW QUESTIONS

UIL Computer Science for Dummies by Jake Warren and works from Mr. Fleming

About The Tutorial. Audience. Prerequisites. Copyright & Disclaimer

Keywords are identifiers having predefined meanings in C programming language. The list of keywords used in standard C are : unsigned void

Example of a Java program

PROGRAMMING IN C PROGRAMMING IN C CONTENT AT A GLANCE

java.util.scanner Here are some of the many features of Scanner objects. Some Features of java.util.scanner

Chapter 7D The Java Virtual Machine

Chapter 1 Java Program Design and Development

ECE 122. Engineering Problem Solving with Java

Object-Oriented Programming in Java

Embedded Systems. Review of ANSI C Topics. A Review of ANSI C and Considerations for Embedded C Programming. Basic features of C

Chapter 2 Introduction to Java programming

Chapter 2 Elementary Programming

Objective-C Tutorial

AP Computer Science Java Subset

Java programming for C/C++ developers

JDK 1.5 Updates for Introduction to Java Programming with SUN ONE Studio 4

PART-A Questions. 2. How does an enumerated statement differ from a typedef statement?

Secrets of printf. 1 Background. 2 Simple Printing. Professor Don Colton. Brigham Young University Hawaii. 2.1 Naturally Special Characters

Computer Programming Tutorial

SYMETRIX SOLUTIONS: TECH TIP August 2015

Introduction to Python

CS1020 Data Structures and Algorithms I Lecture Note #1. Introduction to Java

An Introduction to Computer Science

CS 141: Introduction to (Java) Programming: Exam 1 Jenny Orr Willamette University Fall 2013

Base Conversion written by Cathy Saxton

Inside the Java Virtual Machine

Programming Microcontrollers in C

6.170 Tutorial 3 - Ruby Basics

A list of data types appears at the bottom of this document. String datetimestamp = new java.sql.timestamp(system.currenttimemillis()).

Perl in a nutshell. First CGI Script and Perl. Creating a Link to a Script. print Function. Parsing Data 4/27/2009. First CGI Script and Perl

Chapter 4: Computer Codes

C++ Essentials. Sharam Hekmat PragSoft Corporation

Thomas Jefferson High School for Science and Technology Program of Studies Foundations of Computer Science. Unit of Study / Textbook Correlation

Building Java Programs

Object Oriented Software Design

Introduction to Java Lecture Notes. Ryan Dougherty

AP Computer Science Java Mr. Clausen Program 9A, 9B

Simple Image File Formats

The Java Virtual Machine (JVM) Pat Morin COMP 3002

Programming in Java Course Technology, a part of Cengage Learning.

Maple Introductory Programming Guide

Object Oriented Software Design

Object-Oriented Design Lecture 4 CSU 370 Fall 2007 (Pucella) Tuesday, Sep 18, 2007

VB.NET Programming Fundamentals

As previously noted, a byte can contain a numeric value in the range Computers don't understand Latin, Cyrillic, Hindi, Arabic character sets!

13 Classes & Objects with Constructors/Destructors

Java Programming Fundamentals

1.00 Lecture 1. Course information Course staff (TA, instructor names on syllabus/faq): 2 instructors, 4 TAs, 2 Lab TAs, graders

Programming and Data Structures with Java and JUnit. Rick Mercer

Introduction to Data Structures

Habanero Extreme Scale Software Research Project

Programming Languages CIS 443

Chapter 2. println Versus print. Formatting Output withprintf. System.out.println for console output. console output. Console Input and Output

CS170 Lab 11 Abstract Data Types & Objects

Appendix K Introduction to Microsoft Visual C++ 6.0

Scanner. It takes input and splits it into a sequence of tokens. A token is a group of characters which form some unit.

MULTIPLE CHOICE. Choose the one alternative that best completes the statement or answers the question.

Computer Programming I

J a v a Quiz (Unit 3, Test 0 Practice)

Free Java textbook available online. Introduction to the Java programming language. Compilation. A simple java program

EE 261 Introduction to Logic Circuits. Module #2 Number Systems

Classes and Objects in Java Constructors. In creating objects of the type Fraction, we have used statements similar to the following:

Introduction to Java Applets (Deitel chapter 3)

Free Java textbook available online. Introduction to the Java programming language. Compilation. A simple java program

Lecture 22: C Programming 4 Embedded Systems

Chapter 5 Names, Bindings, Type Checking, and Scopes

Transcription:

JAVA Programming Language Lecture 2:Variables and Data Types Chengwei Zhang ( 张成伟 ) School of Electronic Information and Communications Huazhong University of Science and Technology Mar. 2015

Outline Quick View to Java Application Variables Primitive Data Types Practice Summary 2

Quick View to Java Application 1 // Sample 01: Welcome1.java 2 // Text-printing program. 3 4 public class Welcome1 { 5 6 // main method begins execution of Java application 7 public static void main( String[] args ) 8 { 9 System.out.println( "Welcome to Java Programming!" ); 10 11 } // end method main 12 13} // end class Welcome01 Output: Welcome to Java Programming! 3

Quick View to Java Application Output: Welcome to Java Programming! 10 March 2015 Java Programming Language 4

Quick View to Java Application 1 // Sample 01: Welcome1.java Comments start with: // Comments ignored during program execution Document and describe code Provides code readability Traditional comments: /*... */ /* This is a traditional comment. It can be split over many lines */ 2 // Text-printing program. Another line of comments Note: line numbers not part of program, added for reference 5

Quick View to Java Application 3 Blank line Makes program more readable Blank lines, spaces, and tabs are white-space characters Ignored by compiler 4 public class Welcome01 { Begins class declaration for class Welcome01 Every Java program has at least one user-defined class Keyword: words reserved for use by Java class keyword followed by class name Naming classes: capitalize every word SampleClassName, UndergraduateStudent, 6

Quick View to Java Application 4 public class Welcome01 { Name of class called identifier Series of characters consisting of letters, digits, underscores ( _ ) and dollar signs ( $ ) Does not begin with a digit, has no spaces Examples: Welcome1, $value, _value, button7 7button is invalid Java is case sensitive (capitalization matters) a1 and A1 are different 7

Quick View to Java Application 4 public class Welcome01 { Saving files File name must be class name with.java extension Welcome1.java Left brace { Begins body of every class Right brace ends declarations (line 13) 7 public static void main( String args[] ) Part of every Java application Applications begin executing at main Parenthesis indicate main is a method Java applications contain one or more methods 8

Quick View to Java Application 7 public static void main( String args[] ) Exactly one method must be called main Methods can perform tasks and return information void means main returns no information 8 { Left brace begins body of method declaration Ended by right brace } (line 11) 9

Quick View to Java Application 9 System.out.println( "Welcome to Java Programming!" ); Instructs computer to perform an action Prints string of characters String - series characters inside double quotes White-spaces in strings are not ignored by compiler System.out Standard output object Print to command window (i.e., MS-DOS prompt) Method System.out.println Displays line of text Argument inside parenthesis This line known as a statement Statements must end with semicolon ; 10

Quick View to Java Application 11 } // end method main Ends method declaration 13 } // end class Welcome01 Ends class declaration Can add comments to keep track of ending braces Lines 8 and 9 could be rewritten as: Remember, compiler ignores comments Comments can start on same line after code 11

Quick View to Java Application Compiling a program Open a command prompt window, go to directory where program is stored Type javac Welcome1.java If no errors, Welcome1.class created Has bytecodes that represent application Bytecodes passed to Java interpreter 12

Quick View to Java Application Executing a program on CMD model Type java Welcome01 Interpreter loads.class file for class Welcome1.class extension omitted from command Interpreter calls method main Executing Welcome1 in a Microsoft Windows Command Prompt. 13

A modified Welcome01 sample 10 March 2015 Java Programming Language 14

VARIABLES 15

What is variables? Variables are locations in memory in which values can be stored. Every variable has a name, a type, a size and a value. Before you can use a variable, you have to declare it. After it is declared, you can then assign values to it. 16

Variable Types The variable type can be one of the three things: One of the eight basic primitive data types The name of a class An array of any kind class or primitive data type 17

Three kinds of variables local variables (discussed in Lecture 2) instance variables (discussed in Lecture 4) class variables (discussed in Lecture 4) no global variables? (discussed in Lecture 4) Why Java has no global variables? 10 March 2015 Java Programming Language 18

Local variables Inside method definitions or code segments Store information needed by a single method or some segment code 19

Instance variables (in Lecture 4) Attributes or the state of a particular object Store information needed by multiple methods in the object Have different values for each object 20

Class variables (in Lecture 4) Similar to instance variables Apply to all that class s instances Apply to the class itself Shared among the objects of the same class 21

Steps for using variables Declare variable Initiate variables value before using them Use the variables in a right way 10 March 2015 Java Programming Language 22

Declaring variables Variable declarations consist of a type and a variable name, e.g.: int count; String name; boolean validated; The variables with the same type can be stringed together, e.g.: double x, y, z; String fname, lname; Variable declarations can go anywhere in a method definition Most commonly declared in the beginning of the definition before they are used 23

Variables initial value The initial value of a variable can be given in declaration, e.g.: int a = 10, b = 5; String myname = Zhang Chengwei ; Only the last variable has initial value if there are multiple variables on the same line, e.g.: double x, y, z = 10.0; 24

Notes about variable names Case-sensitive Zhang is not same to zhang. Meaningful names int countofstudentnumber; boolean currentweatherisgood; int intuserage; String stradminname; 25

PRIMITIVE DATA TYPES 26

Eight primitive data types byte short int long char float double boolean 27

Integer Type Width Size(Value Range) long 64-9223372036854775808~9223372036854775807 (-2 63 ~ 2 63-1) int 32-2147483648~2147483647 (-2 31 ~ 2 31-1) short 16-32768~32767(-2 15 ~ 2 15-1) byte 8-128~127 Octal 03, 07 Hexadecimal 0xab 0X1a 10 March 2015 Does Java has no unsigned type s? Java Programming Language 28

Floating-point types Type Width Size double 64 1.7e-308~1.7e+308 float 32 3.4e-038~3.4e+038 18.0f 18.0d = 18.0 18. = 1.8e1 =.18E2 A float value can be assigned to a double variable. A double value can not be assigned to a float variable 29

Char Unicode provides a unique number for every character, no matter what the platform, no matter what the program, no matter what the language. Incorporating Unicode into client-server or multi-tiered applications and websites offers significant cost savings over the use of legacy character sets. Width Size ASCII 7 0~127 ISO-Latin-1 8 0~255 Unicode 16 0~65536 30

ASCII character set 31

Special codes Escape Meaning Escape Meaning \n Newline \ Single quote \t Tab \ Double quote \b Backspace \ddd Octal \r Carriage return \xdd Hexadecimal \f Formfeed \udddd Unicode character \\ Backslash 32

Boolean types Type boolean Value true/false 33

OPERATORS 34

Five Basic Arithmetic Operators Java operation Arithmetic operator Algebraic expression Addition + f + 7 f + 7 Subtraction - p - c p - c Multiplication * bm b * m Division / x / y or x y x / y Java expression Modulus (Remainder) % r mod s r % s 35

Assignment Operator Arithmetic Compound Assignment Operators int a = 5, b = 3, c; Assignment operator Sample expression a =? += (addition) a += b 8 -= a -=b 2 *= a *= b 15 /= a /= b 1 %= a %= b 2 36

Type conversion Widening conversion Truncation 37

Type promoting byte int short int char int one is long, result is long one is float, result is float one is double, result is double 38

Comparison operators Equality operators ==!= Relational operators > < >= <= Must be inserted into the condition statement if, while, for 10 March 2015 Java Programming Language 39

Equality operators ==!= e.g. x == y x is equal to y. e.g. x!= y x is not equal to y. 40

Relational operators > e.g. x > y x is greater than y. < e.g. x < y x is less than y. >= e.g. x >= y x is greater than or equal to y. <= e.g. x <= y x is less than or equal to y. 41

Logical operators &, &&,! ^ 42

Logical operators - AND && (Condition And Operator) e.g. if (salary > 500) && (age++ >= 30) The expression will be true only if both operands tests are also true. & (Boolean Logical And Operator) e.g. if (salary > 500) & (age++ >= 30) The expression will be true only if both operands tests are also true. What s the difference between the two operators? 43

Logical operators - OR (Condition OR Operator) e.g. if (salary > 500) (age++ >= 30) The expression will be true at least one of operands tests is true. (Boolean Logical OR Operator) e.g. if (salary > 500) (age++ >= 30) The expression will be true at least one of operands tests is true. What s the difference between the two operators? 44

CONTROL STRUCTURES 45

Control Structures Sequential execution Program statements execute one after the other Transfer of control Three control statements can specify order of statements Sequence structure Selection structure Repetition structure Activity diagram Models the workflow Action-state symbols Transition arrows 10 March 2015 Java Programming 46 Language

No goto The finger of blame was pointed at the goto statement The notion of so-called structured programming became almost synonymous with goto elimination Java does not have a goto statement; however, goto is a reserved word and should not be used in a Java program 47

Homework 10 March 2015 Java Programming Language 48

Thanks 2015-3-9