Introduction to Computer Science I Variables, Primitive Data Types, - - PowerPoint PPT Presentation

introduction to computer science i
SMART_READER_LITE
LIVE PREVIEW

Introduction to Computer Science I Variables, Primitive Data Types, - - PowerPoint PPT Presentation

Introduction to Computer Science I Variables, Primitive Data Types, Expressions Janyl Jumadinova 29-31 January, 2018 Binary Numbers Binary number system has two digits (0 and 1) 2/19 Binary Numbers Binary number system has two digits


slide-1
SLIDE 1

Introduction to Computer Science I

Variables, Primitive Data Types, Expressions

Janyl Jumadinova 29-31 January, 2018

slide-2
SLIDE 2

Binary Numbers

◮ Binary number system has two digits (0 and 1) 2/19

slide-3
SLIDE 3

Binary Numbers

◮ Binary number system has two digits (0 and 1) ◮ Bit - single binary digit 2/19

slide-4
SLIDE 4

Binary Numbers

◮ Binary number system has two digits (0 and 1) ◮ Bit - single binary digit ◮ Binary number system is base 2 2/19

slide-5
SLIDE 5

Binary Numbers

N bits can represent 2N unique items

3/19

slide-6
SLIDE 6

Variables

◮ Variable is a name for a memory’s location where a data value

is stored.

◮ Variable Declaration allows the compiler to reserve space in the

main memory that is large enough for the specified type int count;

◮ Variable Assignment assigns a value to the variable

count = 0;

4/19

slide-7
SLIDE 7

Variables

◮ Variable is a name for a memory’s location where a data value

is stored.

◮ Variable Declaration allows the compiler to reserve space in the

main memory that is large enough for the specified type int count;

◮ Variable Assignment assigns a value to the variable

count = 0;

◮ Must give a value to the variable before using it. 4/19

slide-8
SLIDE 8

Java Identifiers

◮ reserved keywords (class, public, static, void) ◮ Java classes, methods, variables: words we chose or make up

when writing a program System, println, main, args

5/19

slide-9
SLIDE 9

Java Identifiers

◮ reserved keywords (class, public, static, void) ◮ Java classes, methods, variables: words we chose or make up

when writing a program System, println, main, args

Identifier

a letter followed by zero or more letters (including $ and ) and digits

5/19

slide-10
SLIDE 10

Identifiers

  • An identifier may be any descriptive sequence of uppercase and

lowercase letters, numbers, or the underscore and dollar-sign characters.

6/19

slide-11
SLIDE 11

Identifiers

  • An identifier may be any descriptive sequence of uppercase and

lowercase letters, numbers, or the underscore and dollar-sign characters.

  • Ex: Average, count, num1, $test, this is fine

6/19

slide-12
SLIDE 12

Identifiers

  • An identifier may be any descriptive sequence of uppercase and

lowercase letters, numbers, or the underscore and dollar-sign characters.

  • Ex: Average, count, num1, $test, this is fine

6/19

slide-13
SLIDE 13

Identifiers

◮ Identifiers must start with a letter, a currency character ($), or

a connecting character such as the underscore ( ).

7/19

slide-14
SLIDE 14

Identifiers

◮ Identifiers must start with a letter, a currency character ($), or

a connecting character such as the underscore ( ).

◮ Identifiers cannot start with a number. 7/19

slide-15
SLIDE 15

Identifiers

◮ Identifiers must start with a letter, a currency character ($), or

a connecting character such as the underscore ( ).

◮ Identifiers cannot start with a number. ◮ After the first character, identifiers can contain any combination

  • f letters, currency characters, connecting characters, or

numbers.

7/19

slide-16
SLIDE 16

Identifiers

◮ Identifiers must start with a letter, a currency character ($), or

a connecting character such as the underscore ( ).

◮ Identifiers cannot start with a number. ◮ After the first character, identifiers can contain any combination

  • f letters, currency characters, connecting characters, or

numbers.

◮ There is no limit to the number of characters an identifier can

contain.

7/19

slide-17
SLIDE 17

Identifiers

◮ Identifiers must start with a letter, a currency character ($), or

a connecting character such as the underscore ( ).

◮ Identifiers cannot start with a number. ◮ After the first character, identifiers can contain any combination

  • f letters, currency characters, connecting characters, or

numbers.

◮ There is no limit to the number of characters an identifier can

contain.

◮ You can’t use a Java keyword as an identifier. 7/19

slide-18
SLIDE 18

Identifiers

◮ Identifiers must start with a letter, a currency character ($), or

a connecting character such as the underscore ( ).

◮ Identifiers cannot start with a number. ◮ After the first character, identifiers can contain any combination

  • f letters, currency characters, connecting characters, or

numbers.

◮ There is no limit to the number of characters an identifier can

contain.

◮ You can’t use a Java keyword as an identifier. ◮ Identifiers in Java are case-sensitive; foo and FOO are two

different identifiers.

7/19

slide-19
SLIDE 19

Java keywords

8/19

slide-20
SLIDE 20

Literal

A constant value in Java is created by using a literal representation

  • f it.

◮ 100 ( integer literal ) ◮ 98.6 ( float literal ) ◮ ′X ′ ( character literal ) ◮ ‘‘This is a test’’ ( String literal ) 9/19

slide-21
SLIDE 21

Constants

◮ Constants hold the same value during their existence. ◮ Can use a keyword final before the type and name of the

variable:

  • always contains the same value.

◮ final int MAX BUDGET = 1000 10/19

slide-22
SLIDE 22

Data Types

◮ Data stored in memory is a string of bits (0 or 1) 11/19

slide-23
SLIDE 23

Data Types

◮ Data stored in memory is a string of bits (0 or 1) ◮ How the computer interprets the string of bits depends on the

context.

◮ In Java, we must make the context explicit by specifying the

type of the data.

11/19

slide-24
SLIDE 24

Data Types

◮ Java has two categories of data:

primitive data (e.g., number, character)

  • bject data (programmer created types)

◮ There are 8 primitive data types: byte, short, int, long,

float, double, char, boolean

◮ Primitive data are only single values; they have no special

capabilities.

12/19

slide-25
SLIDE 25

Primitive Data Types

◮ integers: byte, short, int, long ◮ floating point: float, double ◮ characters: char ◮ booleans: boolean 13/19

slide-26
SLIDE 26

Common Primitive Data Types

Type Description Example of Literals int integers (whole numbers) 42, 60634, -8 double real numbers 0.039, -10.2 char single characters ’a’, ’B’, ’&’, ’6’ boolean logical values true, false

14/19

slide-27
SLIDE 27

Range of Values

Type Storage Range of Values int 32 bits

  • 2,147,483,648 to 2,147,483,647

double 64 bits ±10−45 to ±1038 char 16 bits = 2 bytes 0 to 216 or \u0000 to \uFFFF boolean 1 bit NA

15/19

slide-28
SLIDE 28

Expression

Expression is a combination of one or more operators (+, −, %, ...) and operands (literals, constants, variables,...)

16/19

slide-29
SLIDE 29

Order of Precedence

◮ Operators are evaluated in an expression according to the rules

  • f precedence.

17/19

slide-30
SLIDE 30

Order of Precedence

◮ Operators are evaluated in an expression according to the rules

  • f precedence.

◮ Operators within ( ) are evaluated first. ◮ *, /, % evaluated next (L to R). ◮ +, - evaluated last (L to R). 17/19

slide-31
SLIDE 31

Scanner

◮ The Scanner class in the java.util package is a simple text

scanner which can parse primitive types and strings

◮ We can use the Scanner class to get the input from the

terminal

◮ We must create an instance of the Scanner as:

Scanner name = new Scanner (System.in) where name is the name you choose for your instance of the Scanner

18/19

slide-32
SLIDE 32

Scanner Methods

◮ next() : get the next word (token) as a String ◮ nextLine() : get a line of input as a String ◮ nextInt() : get an integer ◮ nextDouble() : get a double value 19/19