objectives
play

Objectives become familiar with Java primitive types Primitive - PDF document

Objectives become familiar with Java primitive types Primitive Types, Strings, (numbers, characters, etc.) learn about assignment statements and and Console I/O expressions learn about strings Chapter 2 become familiar with


  1. Objectives • become familiar with Java primitive types Primitive Types, Strings, (numbers, characters, etc.) • learn about assignment statements and and Console I/O expressions • learn about strings Chapter 2 • become familiar with classes, methods, and objects • learn about simple keyboard input and screen output Outline Variables and Values • Primitive Types and Expressions • Variables store data such as numbers and letters. • The Class String – Think of them as places to store data. • Keyboard and Screen I/O – They are implemented as memory locations. • Documentation and Style • The data stored by a variable is called its value. – The value is stored in the memory location. • Its value can be changed. Naming and Declaring Variables and Values Variables • variables • Choose names that are helpful such as count numberOfBaskets or speed , but not c or s . eggsPerBasket • When you declare a variable, you provide its totalEggs • assigning values name and type. eggsPerBasket = 6; int numberOfBaskets, eggsPerBasket; eggsPerBasket = eggsPerBasket - 2; • A variable’s type determines what kinds of values it can hold ( int , double , char , etc . ). • A variable must be declared before it is used. 1

  2. Syntax and Examples Types in Java • syntax • A class type type variable_1, variable_2, …; – a class of objects and has both data and methods. – “Think Whirled Peas” is a value of class type String • examples • A primitive type int styleChoice, numberOfChecks; – simple, nondecomposable values such as an double balance, interestRate; individual number or individual character. char jointOrIndividual; – int , double , and char are primitive types. Naming Conventions Where to Declare Variables • Class types • Declare a variable – begin with an uppercase letter (e.g. String ). – just before it is used or – at the beginning of the section of your program • Primitive types that is enclosed in {}. – begin with a lowercase letter (e.g. int ). public static void main(String[] args) • Variables of both class and primitive types { /* declare variables here */ – begin with a lowercase letters (e.g. myName, myBalance ). … – Multiword names are “punctuated” using uppercase letters. } Java Identifiers Java Identifiers, cont. • An identifier • identifiers may not contain any spaces, dots ( . ), asterisks ( * ), or other characters: – a name, such as the name of a variable. util.* (not allowed) • Identifiers may contain only 7-11 netscape.com • Identifiers can be arbitrarily long. – letters – digits (0 through 9) • Since Java is case sensitive , stuff , Stuff , – the underscore character (_) and STUFF are different identifiers. – and the dollar sign symbol ($) which has a special meaning – but the first character cannot be a digit. 2

  3. Keywords or Reserved Words Primitive Types • Words such as if are called keywords or • four integer types ( byte , short , int , and reserved words and have special, predefined long ) meanings. – int is most common • Keywords cannot be used as identifiers. • two floating-point types ( float and double ) – double is more common • See Appendix 1 for a complete list of Java keywords. • one character type ( char ) • other keywords: int , public , class • one boolean type ( boolean ) • Appendix 1 Primitive Types, cont. Examples of Primitive Values • integer types 0 -1 365 12000 • floating-point types 0.99 -22.8 3.14159 5.0 • character type `a` `A` `#` ` ` • boolean type true false Assignment Statements Assignment Statements, cont. • An assignment statement is used to assign a • Syntax value to a variable. variable = expression ; answer = 42; where expression can be • The “equal sign” is called the assignment – another variable, operator. – a literal or constant (such as a number), • We say, “The variable named answer is – or something more complicated which combines variables and literals using operators (such as + assigned a value of 42,” or more simply, and - ) “ answer is assigned 42.” 3

  4. Assignment Examples Assignment Evaluation amount = 3.99; • The expression on the right-hand side of the firstInitial = ‘W’; assignment operator ( = ) is evaluated first. score = numberOfCards + handicap; • The result is used to set the value of the eggsPerBasket = eggsPerBasket - 2; variable on the left-hand side of the (last line looks weird in mathematics, why?) assignment operator. score = numberOfCards + handicap; eggsPerBasket = eggsPerBasket - 2; Specialized Assignment Simple Screen Output Operators System.out.println(“The count is “ + count); • Assignment operators can be combined with arithmetic operators (including - , * , / , and % , • outputs the Sting literal “ The count is “ followed discussed later). by the current value of the variable count . amount = amount + 5; • + means concatenation if one argument is a can be written as string amount += 5; yielding the same results. (an example of which of the three properties of OO languages?) Simple Input Simple Input, cont. • Sometimes the data needed for a • Data can be entered from the keyboard using computation are obtained from the user at run Scanner keyboard = time. new Scanner(System.in); followed, for example, by • Keyboard input requires eggsPerBasket = keyboard.nextInt(); import java.util.* which reads one int value from the keyboard at the beginning of the file. and assigns it to eggsPerBasket . 4

  5. Simple Input, cont. Number Constants • class EggBasket2 • Literal expressions such as 2 , 3.7 , or ’y’ are called constants . • Integer constants can be preceded by a + or - sign, but cannot contain commas. • Floating-point constants can be written – with digits after a decimal point or – using e notation. e Notation Assignment Compatibilities • Java is said to be strongly typed. • e notation is also called scientific notation or – You can’t, for example, assign a floating point floating-point notation. value to a variable declared to store an integer. • examples • Sometimes conversions between numbers – 865000000.0 can be written as 8.65e8 are possible. – 0.000483 can be written as 4.83e-4 doubleVariable = 7; • The number in front of the e does not need to doubleVariable = intVariable ; contain a decimal point, eg. 4e-4 is possible even if doubleVariable is of type double , for example. Assignment Compatibilities, Type Casting cont. • A value of one type can be assigned to a • A type cast creates a value in a new type variable of any type further to the right from the original type. • For example, byte --> short --> int --> long --> float --> double double distance; but not to a variable of any type further to the distance = 9.0; left. int points; points = (int)distance; • You can assign a value of type char to a (illegal without (int) ) variable of type int . 5

  6. Type Casting, cont. Characters as Integers • Characters are actually stored as integers • The value of (int)distance is 9 , but the value according to a special code of distance , both before and after the cast, is – each printable character (letter, number, 9.0 . punctuation mark, space, and tab) is assigned a • The type of distance does NOT change different integer code and remains float . – the codes are different for upper and lower case – for example 97 may be the integer value for ‘a’ • Any nonzero value to the right of the decimal and 65 for ‘A’ point is truncated, rather than rounded. • ASCII and Unicode are common character codes Unicode Character Set ASCII/Unicode • Most programming languages use the ASCII 32 33 34 35 36 37 38 39 40 41 character set. ! “ # $ % & ‘ ( ) • Java uses the Unicode character set which includes the ASCII character set (Appendix 3) 48 … 57 … 65 … 90 … 97 … 122 • The Unicode character set includes characters from many different alphabets 0 9 A Z a z other than English (but you probably won’t use them). Casting a char to an int Initializing Variables • Casting a char value to int produces the • A variable that has been declared, but no yet ASCII/Unicode value given a value is said to be uninitialized. • For example, what would the following • Uninitialized class variables have the value display? null . char answer = ’y’; • Uninitialized primitive variables may have a System.out.println(answer); System.out.println((int)answer); default value. • >y • It’s good practice not to rely on a default >121 value, which could be arbitrary. > 6

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend