Java Identifiers. Java Language Essentials. Java Keywords. Java Applications have Class. Slide Set 2: Java Essentials. Copyright 2012 R.M.

Similar documents
Expressions and Data Types CSC 121 Spring 2015 Howard Rosenthal

CS5000: Foundations of Programming. Mingon Kang, PhD Computer Science, Kennesaw State University

Expressions and Data Types CSC 121 Fall 2015 Howard Rosenthal

BIT Java Programming. Sem 1 Session 2011/12. Chapter 2 JAVA. basic

Java Notes. 10th ICSE. Saravanan Ganesh

Full file at

Program Fundamentals

3. Java - Language Constructs I

CHAPTER 2 Java Fundamentals

JAVA Programming Fundamentals

COMP 202 Java in one week

UNIT- 3 Introduction to C++

Expressions and Data Types CSC 121 Spring 2017 Howard Rosenthal

Visual C# Instructor s Manual Table of Contents

Declaration and Memory

Language Fundamentals Summary

Object oriented programming. Instructor: Masoud Asghari Web page: Ch: 3

CSC 1214: Object-Oriented Programming

Basics of Java Programming

Data and Variables. Data Types Expressions. String Concatenation Variables Declaration Assignment Shorthand operators. Operators Precedence

An overview of Java, Data types and variables

Getting started with Java

5/3/2006. Today! HelloWorld in BlueJ. HelloWorld in BlueJ, Cont. HelloWorld in BlueJ, Cont. HelloWorld in BlueJ, Cont. HelloWorld in BlueJ, Cont.

Lecture Set 2: Starting Java

Operators. Java operators are classified into three categories:

Lecture Set 2: Starting Java

Computer Components. Software{ User Programs. Operating System. Hardware

Introduction to C# Applications

4 Programming Fundamentals. Introduction to Programming 1 1

Data Types and Variables in C language

CSCI 2010 Principles of Computer Science. Data and Expressions 08/09/2013 CSCI

Introduction to Programming Using Java (98-388)

Chapter 2: Using Data

Chapter 2: Data and Expressions

BASIC ELEMENTS OF A COMPUTER PROGRAM

CMPT 125: Lecture 3 Data and Expressions

Lecture 2: Variables and Operators. AITI Nigeria Summer 2012 University of Lagos.

Programming and Data Structures

Fundamental of Programming (C)

JOSE LUIS JUAREZ VIVEROS com) has a. non-transferable license to use this Student Guide

Introduction to Java & Fundamental Data Types

3. Java - Language Constructs I

Java Programming. Atul Prakash

Accelerating Information Technology Innovation

Objectives. Chapter 2: Basic Elements of C++ Introduction. Objectives (cont d.) A C++ Program (cont d.) A C++ Program

Chapter 2: Basic Elements of C++

Character Set. The character set of C represents alphabet, digit or any symbol used to represent information. Digits 0, 1, 2, 3, 9

Chapter 2 Elementary Programming

Chapter 2: Basic Elements of C++ Objectives. Objectives (cont d.) A C++ Program. Introduction

Data and Expressions. Outline. Data and Expressions 12/18/2010. Let's explore some other fundamental programming concepts. Chapter 2 focuses on:

Chapter 2: Java Fundamentals

8/23/2014. Chapter Topics. Chapter Topics (2) Parts of a Java Program. Parts of a Java Program. Analyzing The Example. Chapter 2: Java Fundamentals

The Java Language Rules And Tools 3

Chapter 2: Data and Expressions

LESSON 1. A C program is constructed as a sequence of characters. Among the characters that can be used in a program are:

Introduction To Java. Chapter 1. Origins of the Java Language. Origins of the Java Language. Objects and Methods. Origins of the Java Language

6.096 Introduction to C++ January (IAP) 2009

Section 2: Introduction to Java. Historical note

Course Outline. Introduction to java

Programming in C++ 4. The lexical basis of C++

UNIT - I. Introduction to C Programming. BY A. Vijay Bharath

Computer Components. Software{ User Programs. Operating System. Hardware

CSc 10200! Introduction to Computing. Lecture 2-3 Edgardo Molina Fall 2013 City College of New York

Entry Point of Execution: the main Method. Elementary Programming. Learning Outcomes. Development Process

Datatypes, Variables, and Operations

Objects and Types. COMS W1007 Introduction to Computer Science. Christopher Conway 29 May 2003

A Java program contains at least one class definition.

Chapter 2. Elementary Programming

A.A. 2008/09. Why introduce JavaScript. G. Cecchetti Internet Software Technologies

Weiss Chapter 1 terminology (parenthesized numbers are page numbers)

Review: Exam 1. Your First C++ Program. Declaration Statements. Tells the compiler. Examples of declaration statements

Tester vs. Controller. Elementary Programming. Learning Outcomes. Compile Time vs. Run Time

Programming Lecture 3

Chapter 3: Operators, Expressions and Type Conversion

2.1. Chapter 2: Parts of a C++ Program. Parts of a C++ Program. Introduction to C++ Parts of a C++ Program

Elementary Programming

BASIC COMPUTATION. public static void main(string [] args) Fundamentals of Computer Science I

Chapter 2: Data and Expressions

Assoc. Prof. Dr. Marenglen Biba. (C) 2010 Pearson Education, Inc. All rights reserved.

Java Programming Fundamentals. Visit for more.

Objectives. In this chapter, you will:

Ex: If you use a program to record sales, you will want to remember data:

The MaSH Programming Language At the Statements Level

Chapter 2. Lexical Elements & Operators

Java Basic Datatypees

12/22/11. Java How to Program, 9/e. Help you get started with Eclipse and NetBeans integrated development environments.

Chapter 2 Working with Data Types and Operators

2 rd class Department of Programming. OOP with Java Programming

Basic Elements of C. Staff Incharge: S.Sasirekha

Java Language Basics: Introduction To Java, Basic Features, Java Virtual Machine Concepts, Primitive Data Type And Variables, Java Operators,

Chapter 2 ELEMENTARY PROGRAMMING

Tools : The Java Compiler. The Java Interpreter. The Java Debugger

Lexical Considerations

The C++ Language. Arizona State University 1

Lecture 2 Tao Wang 1

Java+- Language Reference Manual

Fundamental Data Types. CSE 130: Introduction to Programming in C Stony Brook University

C Language Part 1 Digital Computer Concept and Practice Copyright 2012 by Jaejin Lee

Fundamentals of Programming

JAVA Ch. 4. Variables and Constants Lawrenceville Press

Transcription:

Java Language Essentials Java is Case Sensitive All Keywords are lower case White space characters are ignored Spaces, tabs, new lines Java statements must end with a semicolon ; Compound statements use curly braces { } Java Language Comments Single-line // Comment goes to end of line Multi-line comments /* This is a comment */ Create a title block at beginning of program to describe program Describe purpose of unusual code Use descriptive identifiers in code Copyright 2012 R.M. Laurie 1 Java Identifiers Required Identifier Naming Rules Identifiers are case sensitive Begin each identifier with a letter, _underscore, or $ dollar sign (no numbers) May use numbers for any character after first No spaces allowed, but may use underscore _ Identifier maximum length 255 is characters, but try to use less to minimize input errors Not a Java keyword (approximately 50) public class name must be same as filename.java Suggested Identifier Naming Guidelines Class identifier should be TitleCase without spaces Method identifier should start with lower case and be a verbnoun combination with words in title case Variable identifiers are nouns and begin with lower case letter(s) to indicate variable data type Copyright 2012 R.M. Laurie 2 Java Keywords boolean private for transient char protected continue instanceof byte public do true float static extends false void new class throws short this volatile native double super while implements int interface return import long package throw synchronized abstract switch try const if case catch goto else break finally null Java Applications have Class Java applications are contained in classes Each filename.java file must have one (and only one) public class declaration with same name: 1. // The File Name for this program is Anatomy.java 2. public class Anatomy // Class definition header 3. { // Class definition body begins here 4. public static void main(string[] args) 5. { // main method is starting point of program 6. System.out.println("Anatomy of a class"); 7. } // main method code ends here 8. } // Class definition body ends here Every Java applications contain methods main( ) method is start of program for java public keyword means other classes can access main method static keyword means method can be called from other classes void keyword means method won't return any values final default Every main has an argument args defined as array of strings Copyright 2012 R.M. Laurie 3 Copyright 2012 R.M. Laurie 4 Copyright 2012 R.M. Laurie 1

Output using PrintStream Class Package java.io Multiple classes stored in same directory or folder Class PrintStream contains methods println() // Std output and ends with new line print() // Std output and no new line Usage syntax: System.out.print("Enter Dimes Quantity: "); Escape Characters can be contained in string \" Double quote. \' Single quote. \\ Backslash. \n New line. Go to the beginning of the next line. \r Carriage return. Go to beginning of current line. \t Tab. White space up to the next tab stop. Copyright 2012 R.M. Laurie 5 Escape Character Example How do you print characters with special meaning? For example, how do you print the following string? The word "hard" Would this work? System.out.println( "The word "hard"" ); No, it would give a compiler error - it sees the string The word between the first set of double quotes and is confused by what comes after Use the backslash character, \", to escape the special meaning of the internal double quotes: System.out.println( "The word \"hard\"" ); Copyright 2012 R.M. Laurie 6 Data Value Literals Literals are fixed human-readable values that can not be altered by program Numbers Integer Values are Whole Numbers 1-406 352563 0-32 123456789 Floating Point Values are Real Numbers 5. 0.0-0.015-1.5e-2 157.675 1.57675e2 Character Codes Single Characters 'A' 'a' 'C' '3' '$' '\n' '/' '?' Strings of Characters "ABC" "abc\ndef" "32" "-5.2" "-1.5e-2" Copyright 2012 R.M. Laurie 7 Java Arithmetic Operators Arithmetic Operators Precedence Order is the order the operation Parenthesis ( ) have highest precedence Use parenthesis if order of operation not apparent (Precedence Highest to Lowest) ( ) Defines order of operation - Negative (unary) * / % Multiply, Division, Modulus + - Addition, Subtraction = Assignment Concatenation Operator + For joining "Strings" and 'Characters' "Hot " + "Dog" + '\n' + "That\'s mine\n" Copyright 2012 R.M. Laurie 8 Copyright 2012 R.M. Laurie 2

1. public class OperEx01 5. System.out.println(100.0000); 6. System.out.println(6); 7. System.out.println(3.75); 8. System.out.println(100+25); 9. System.out.println(-100+25); 10. System.out.println(100-25); 11. System.out.println(100*25); 12. System.out.println(-100/25); 13. System.out.println(-100/-25); 14. System.out.println(100/31); 15. System.out.println(100%31); 16. System.out.println(100.0/31.0); 17. System.out.println(1e2%3.1e1); 18. System.out.println(6.5/2.1); 19. System.out.println(6.5%2.1); 20. } 21. } > java OperEx01 100.0 6 3.75 125-75 75 2500-4 4 3 7 3.225806451612903 7.0 3.095238095238095 0.19999999999999973 Integer Expression Mixed Mode Expressions If all numbers are integers then result is integer Real (Floating Point) Expression If any number is floating point (real) then result is floating point (real) number String Expression If any value on either side of the + operator is a string then the operator is concatenation You can force an arithmetic operation by enclosing the Integer or Real Expressions with Parenthesis ASCII: 8-bit, Latin characters (Limited to C++ but Not Java) Both uppercase and lowercase letters Digits 0 to 9 and keyboard symbols $,#.!;@* Unicode: 16-bit, All Language Glyphs, Java! 65,536 different glyphs for all languages Copyright 2012 R.M. Laurie 10 Compound Operations 1. public class OperEx02 > java OperEx02 5. System.out.println(3+5+7); 15 6. System.out.println(5*6+3); 33 7. System.out.println(3+5*6); 33 8. System.out.println(5*(6+3)); 45 9. System.out.println(-6*7%3+2); 2 10. System.out.println(-6*7%(3+2)); -2 11. System.out.println(6*4+3*2); 30 12. System.out.println(6*(4+3)*2); 84 13. System.out.println(6*(4+3*2)); 60 14. System.out.println(100/8*2); 24 15. System.out.println(100%8/3); 1 16. } 17. } Copyright 2012 R.M. Laurie 11 1. public class OperEx03 5. System.out.println(20/3); 6. System.out.println(20./3); 7. System.out.println(3.+9/6); 8. System.out.println(3+9/6.); 9. System.out.println("ABC"+'D'+"EF"); 10. System.out.println("ABC"+'\t'+"EF"); 11. System.out.println("ABC"+'\"'+"EF"); 12. System.out.println("Product = " + 7*5); 13. System.out.println("Quotient = " + 7/5.); 14. System.out.println("Remainder = " + (7%5)); 15. System.out.println("Sum = " + 7+5); 16. System.out.println("Sum = " + (7+5)); 17. System.out.println("Difference = " + (7-5)); 18. System.out.println("23 + 42 = " + 23+42); 19. System.out.println("23 + 42 = " + (23+42)); 20. } 21. } 6 6.666666666667 4.0 4.5 ABCDEF ABC EF ABC"EF Product = 35 Quotient = 1.4 Remainder = 2 Sum = 75 Sum = 12 Difference = 2 23 + 42 = 2342 23 + 42 = 65 Copyright 2012 R.M. Laurie 3

Java Console Application with Variables /* Invoice Application * Author: Robert Laurie * Date: 14 March 2012 * ------------------------------------ * Description: This program will * calculate the invoice price using * a 20% discount */ public class InvoiceApp { public static void main(string[] args) { // VARIABLE DECLARATIONS double ddiscount, dinvoice, dorder = 2000; } // PROCESSING ddiscount = dorder * 0.2; dinvoice = dorder - ddiscount; // DISPLAY RESULTS System.out.println("\nINVOICE APPLICATION"); System.out.println("Order total: $" + dorder); System.out.println("Discount: $" + ddiscount); System.out.println("Invoice total: $" + dinvoice + "\n"); Java Primitive Data Types Primitive data types contain atomic data values, which can not be decomposed into smaller data types. Most common DataTypes are int and double } Copyright 2012 R.M. Laurie 13 Copyright 2012 R.M. Laurie 14 Declaration Statements Variable is a container for data nlength 6 Declaration Statements allocate memory to hold a data in a Variable Specifies Data Type int nlength; Followed by Identifier Terminate Java statement with semicolon ; Optionally, may declare several variables of the same data type (comma separated) int nlength, narea; Optionally, may initialize variables in declaration statement int nlength = 6; Copyright 2012 R.M. Laurie 15 Integer Data Type Declarations int Reserves 32 bits (4 bytes) of RAM memory which can represent: Range of values ± 2 billion 2,147,483,647 to -2,147,483,648 Declaration Examples: int nssn = 390546348; nssn 390546348 int ntotalscore, nclassmedian; ntotalscore nclassmedian int naltitude = -100, ndistance = 50000; naltitude -100 ndistance 50000 Copyright 2012 R.M. Laurie 16 Copyright 2012 R.M. Laurie 4

Long, Short, and Byte Integer Data Types long Reserves 64 bits (8 bytes) of RAM memory Range of values ± 9 quadrillion 9,223,372,036,854,775,807 to -9,223,374,036,854,775,808 long lndistance = -350L; lndistance -350 short Reserves 16 bits (2 bytes) of RAM memory Range of values: +32,767 to -32,768 short sntotal=400, snscore=1; sntotal 400 snscore 1 byte Reserves 8 bits (1 byte) of RAM memory Range of values: +127 to -128 byte bnpercent = 12; bnpercent 12 Copyright 2012 R.M. Laurie 17 Floating Point (Real) Data Types float Reserves 32 bits (4 bytes) of RAM memory Range of values ± 1 x 10 ±38 (7-digit precision) ± 3.4028234 x 10 +38 to ± 1.4012984 x 10-45 float fcash = 257.5F; double Reserves 64 bits (8 bytes) of RAM memory Range of values ± 1 x 10 ±308 ± 1.7697693134862315 x 10 +308 to ± 4.940656458412465 x 10-324 (15-digit precision) double dcash = 257.5, dsavings = 2.5e6; Copyright 2012 R.M. Laurie 18 Precision, Exponential Notation, Atomic Data Precision: Significant digits of a number Significant digits determine Accuracy Fewer digits results in round-off error Exponential notation: Scientific Notation format 63421.0 can be written 6.34210e4 0.00634210 can be written 6.34210e-3 Atomic data versus Composite data Atomic data is complete entity by itself Composite data is composed of multiple data entities that are atomic or composite for 1. public class DataTypeEx01 5. int nnum1 =300, nnum2 = 1000; 6. double dnum4 = 7.0, dnum5 = 10, dnum6; 7. float fnum7 = 7f, fnum8 = 10F, fnum9; 8. System.out.println(nNum1 + " " + nnum2 ); 9. System.out.println(dNum4 + " " + dnum5); 10. System.out.println(nNum2 / nnum1); 11. System.out.println(dNum5 / dnum4); 12. System.out.println(dNum5 / nnum1); 13. System.out.println(nNum2 / dnum4); 14. dnum6 = dnum5 / dnum4; 15. System.out.println(dNum6); 16. fnum9 = fnum8 / fnum7; 17. System.out.println(fNum9); 18. System.out.println("Done"); 19. } example a string of characters Copyright 2012 R.M. Laurie 19 Copyright 2012 R.M. Laurie 20 20. } Number DataType Example 300 1000 7.0 10.0 3 1.4285714285714286 0.0333333333333333 142.85714285714286 1.4285714285714286 1.4285715 Done Copyright 2012 R.M. Laurie 5

Character and Boolean Data Type char Reserves 16 bits (2 bytes) of RAM memory Unsigned integer in range 0 to 65535 representing character Examples: char cgradea = \u0065, cgradeb = 'B'; boolean Reserves 1 bit of RAM memory Usually, 1 byte because smallest addressable memory size Evaluates as true/false 1. public class DataTypeEx02 5. char cgradea = 65, cgradeb = 'B', cgradec = '\u0043'; 6. boolean braining = true; 7. System.out.println(cGradeA + " " +cgradeb+" " + cgradec); 8. System.out.println("Is it Raining? " + braining); 9. } 10. } A B C Is it Raining? true Copyright 2012 R.M. Laurie 21 Primitive Data Types (Size and Range) Type Name Identifer Prefix Literal Postfix Kind of Data Memory Allocated Data Range byte bnvar integer 1 byte -128 to 127 short snvar integer 2 bytes -32768 to 32767 int nvar default integer 4 bytes -2,147,483,648 to 2,147,483,647 long lnvar 123L integer 8 bytes -9,223,372,036,854,775,808 to 9,223,374,036,854,775,808 float fvar 12.5f 12.5F floating point double dvar default floating point char cvar 'A' Single character (Unicode) boolean bvar true or false 4 bytes +/- 3.4028 x 10 +38 to +/- 1.4023 x 10-45 8 bytes +/- 1.767 x 10 +308 to +/- 4.940 x 10-324 2 bytes 65,536 Unicode characters 1 bit not applicable Copyright 2012 R.M. Laurie 22 DataType Literals Literals are fixed human-readable values that can not be altered by program LITERALS DATA TYPE 'A' Char "Hello" String of characters +3 12-123 Integer (no literal) 35000L -35L Long Integer 123.45F -4.1e-2f Float 123.45-4.1e-2 Double (no literal) 0x4F 0x6B 0x21 Hexadecimal (Base 16) 026 001 Octal (Base 8) Copyright 2012 R.M. Laurie 23 Constants and Type Cast Operators Named Constants are immutable; can't change final double TAXRATE_MI = 5.5; final int TOTAL_STATE = 50; final double PI = 3.141592653589793; final float PI = 3.1415927; Type Casting operator changes the data type Syntax: (datatype) expression dhalf = (double) 1 / 2; ndimes = (int) dtotal * 10; dchipvalue = (double) nchips * 20; bncreditcards = (byte) (namex + nvisa + nmc); lntotaldebt = (long) ndebt; ndebt = (int) lntotaldebt; Copyright 2012 R.M. Laurie 24 Copyright 2012 R.M. Laurie 6