background image

Języki Programowania 

 

 

 

Typy i operacje 

 

background image

R. Pełka:  Języki Programowania, 2008 

Primitive types: numerical values 

 Types for numerical values 

 Integers  (-3, 0, 1, 5500, -

29567, …) 

Type 

byte 

Type 

short 

Type 

int 

Type 

long

  

 Real numbers (0.0, -23.45, 4.67093) 

Type 

float 

Type 

double 

 

background image

Integers 

 Different sizes  

 different sizes (# bits) 

allocated in memory for storing the value 

different range of possible values 

 

Type 

# bits 

Range 

byte 

8 bits 

-128 to 127 

short 

16 bits 

-32768 to 32767 

int 

32 bits 

-2147483648 to 2147483647 

long 

64 bits 

-9223372036854775808 to 9223372036854775807 

(L,l - long; O - octal; 0x - hex)  

A. Poniecki

:  Języki Programowania, 2012 

background image

R. 

Pełka:  Języki Programowania, 2008 

Real numbers 

Type 

# bits 

Range

 

float 

32 bits 

- 3.402823466385289 *10 

+38

 to  - 1.401298464 *10 

-45

 (F, f) 

1.401298464324817E-45  to  3.4028234663852886E38(F, f) 

 

double 

64 bits 

-1.7976931348623157*10

+308 

to - 4.94065645841247 *10 

-324 

4.94065645841246544E-324 to  1.7976931348623157E308   

 

 Different sizes  

 different sizes (# bits) 

Floating point numbers (Float and Double) 

Double 

 higher precision (more decimals) 
 smaller and bigger values (smaller and bigger power values) 

 

background image

A. Poniecki:  

Języki Programowania, 2012 

Typy rzeczywiste cd. 

 Typ 

float

     

6, 7 cyfr znaczących 

 

 Typ 

double

  

15 cyfr znaczących  

 

 

np: 

0.125    to    2-3 

 

 w zapisie hex: 

0x1.0p-3 

 

 

wykładnik potęgi  p (podstawa 2)   zamiast  E (10)  

 

 

 

jest przedstawiany w notacji dziesiętnej, natomiast 

 

 

mantysa w hex. 

 

Dla liczb przekraczających zakres: 

 

dodatnia i ujemna nieskończoność  

 

 

double.positive_infinity,  float.negative_infinity 

 

- NaN (ang. Not a Number)  

     

background image

R. Pełka:  Języki Programowania, 2008 

Typy rzeczywiste cd. 

public class LitLicz {  
  public static void main(String[] args) { 

System.out.println( 10 + 0x10 );  

  System.out.println( 10/3 );  
  System.out.println( 10./3 );  
  System.out.println( 10d/3);  
  System.out.println( 2147483648L );  
  System.out.println( 2147483647 + 1 ); 

System.out.println( 2147483647L + 1 ); } } 

 

Wynik działania programu: 

  26 


3.3333333333333335 
3.3333333333333335 
2147483648 
-2147483648 
2147483648

 

 

background image

R. Pełka:  Języki Programowania, 2008 

Arithmetic operators 

 Basic numerical operators 

 

 

 

 

 

 

 

 

 

 

Addition 

─  Subtraction 

Multiplication 

Division 

Remainder (mod) 

 Operators precedence 

 First priority 

*   /    %   

 Second priority 

+  

─ 

 Operators with 

higher

 priority are applied 

first 

 Operators with 

same

 priority are applied from 

left to right 

 

background image

R. Pełka:  Języki Programowania, 2008 

Arithmetic operators 

 Basic numeric operators: 

comparisons 

 

 

 

 

 

 

 

 

 

 

 

 

 

== 

Equality 

Less than 

Greater than 

!= 

Inequality 

<= 

Less than or equal to 

>=   Greater than or equal to 

background image

Type conversions 

Implicit

 conversion 

(niejawne konwersje

   

z możliwością powstania niedokładności

 

 

 

Explicit

 conversion: 

casting   

(jawne konwersje, 

rzutowanie) 

   

(

<type>

)

 <expression> 

 Example 

 

   

double f; 

  

int i = 3; 

  

f =(double) i; 

    

y = (byte) 300; 

 

byte  

short  

int  

double  

float  

long  

A. Poniecki

:  Języki Programowania, 2012 

background image

10 

Type conversions 

Przykład niejawnej konwersji typów 

byte 

(do 

int

): 

 

      

 

byte a, byte b=4. byte c=5; 

 

 

w operacji    

 a = b * c; 

 

zgłoszony  zostaje błąd. 

 

 

 

Prawidłowa postać równania: 

 

 

 

 

 a = (byte) (b * c);

 

 

 

     

A. Poniecki:  

Języki Programowania, 2012 

background image

R. Pełka:  Języki Programowania, 2008 

11 

Variable declaration 

 Arithmetic Variable Declaration 

 
byte    b; 
int     i; 
short   j; 
long    k; 
 
float   f; 
double  d; 
 

 If no values assigned: 

0 by default 

 

 

v

ariable’s type 

v

ariable’s name 

background image

R. Pełka:  Języki Programowania, 2008 

12 

Variable names and declarations 

Nazwy jako 

ciągi znaków: 

sekwencja jednego lub więcej znaków.  

początek od litery lub znaku ‘_’ 

może zawierać wyłącznie litery, cyfry, znak '_', znak '$' 

 

i i1  i2  anInteger  afloat 

 

Gdzie umieszczać definicje zmiennych? 

Przed początkiem metod. 

 

 

background image

R. Pełka:  Języki Programowania, 2008 

13 

Variable assignments 

Numerical variables assignment 

<varname> = <arithmetic_expression>

  

     

 

 

 

assignment operator 

arithmetic_expression is one of: 

 Arithmetic value 
 Variable of numeric primitive type 
 Combination of values and/or variables with numeric 

operators 

Expression evaluates to 

same

 type as the variable type 

 

Warning

: implicit conversions may change types and 

 

results. 

 

 

background image

R. Pełka:  Języki Programowania, 2008 

14 

Przypisanie do zmiennych 

Assignment 

Evaluated as 

double f = 13.0 / 5.0; 

2.6 

double f = 13 / 5; 

2.0   // implicit conversion (int -> double) 

int i = 13 / 5; 

long l = 200; 

200  // implicit conversion (int -> long) 

background image

R. Pełka:  Języki Programowania, 2008 

15 

Inkrementacja i dekrementacja 

The increment and decrement operators use only 
one operand 

The increment operator (++) adds one to its operand 

The decrement operator (--) subtracts one from its 
operand 

The statement 

   

 

count++; 

  is functionally equivalent to 
   

 

count = count + 1; 

 

background image

R. Pełka:  Języki Programowania, 2008 

16 

Inkrementacja i dekrementacja - uwagi 

The increment and decrement operators can be applied in 

postfix

 form

   

count++

 

uses old value in the expression,  

 

 

 

then increments 

or 

prefix

 form

   

++count

 

increments then uses new value in  

 

 

 

the expression  

When used as part of a larger expression, the two forms can 
have different effects 

Because of their subtleties, the increment and decrement 
operators 

should be used with care 

x++ + x++ ≠ 2*(x++) 

x++ + x ≠ x + x++ 

Such effects are known as 

side-effects

.  

 

Take care of them! 

background image

R. Pełka:  Języki Programowania, 2008 

17 

Przypisanie i operator jednocześnie 

There are many assignment operators in Java, including 
the following: 

Operator 

 

+= 
-= 
*= 
/= 
%=
 

Example 

 

x += y 
x -= y 
x *= y 
x /= y 
x %= y
 

Equivalent To 

 

x = x + y 
x = x - y 
x = x * y 
x = x / y 
x = x % y 

background image

R. Pełka:  Języki Programowania, 2008 

18 

Numerical values are not Strings 

 What is the difference between: 

int i = 35; 
String s = ”35”; 
 
 Assuming the above declarations, are these assignments legal? 
  i = ”35”; 
  i = s;  
  s = i; 
 

of course, not 

String 

int 

background image

R. Pełka:  Języki Programowania, 2008 

19 

Typ prymitywny: char 

 character 

 ‘a’, ‘A’, ‘_’,  ‘9’, ‘?’, ‘£’, ‘ ’, ‘\

 ’ 

Type 

char 

possible values: all 2

16

 (65536) 

characters encodable in the Unicode 
character set (16-bit values) 

considered numeric values 

 

 

 

 

Java supports the UNICODE but many 
software tools like editors and operating 
systems do not support UNICODE 

When c is the char, then c++ is the next 
character and c

– is the previous one 

Non printable chars eg.: \n, \t, \r 

Type  # bits  Values 

char 

16 

0x0  to 0xffff  (hexadecimal notation) 

char code \uF9BC 

background image

R. Pełka:  Języki Programowania, 2008 

20 

UNICODE 

Unicode

 (zwany czasem po polsku Unikod) 

-  komputerowy  zestaw 

znaków  mający  w 

zamierzeniu 

obejmować  wszystkie  pisma 

używane  na  świecie.  Definiują  go  dwa 
standardy - Unicode oraz ISO 10646. Znaki 
obu 

standardów  są  identyczne.  Standardy 

te 

różnią  się  w  drobnych  kwestiach,  m.in. 

Unicode 

określa sposób składu. 

 

background image

21 

Od Java 5.0 zestaw unicode został rozszerzony. 

Współrzędne kodowe (code points) zostały pogrupowane 
w 17 przestrzeniach numeracyjnych (code planes

Pierwsza zwana jest wielojęzyczną (BMP):  

 U+0000 do U+FFFF 

 

Pozostałe 16 stanowią obszary surogatów: 

 U+10000 do U+10FFFF 

Kodowane są jako pary jednostek kodowych (z 2048 
nieużywanych w BMP) 

Np. znak o współrzędnej U+1056B:      U+D835 oraz U+DD6B 

Niepolecany typ do wykorzystania w programach.

 

A. Poniecki

:  Języki Programowania, 2012 

Typ char cd. 

background image

R. Pełka:  Języki Programowania, 2008 

22 

char 

– comparisons 

 Same as for other primitive types 

 

 

 

 

 

 

 

 

 

 

 

 

 

== 

Equality 

Less than 

Greater than 

!= 

Inequality 

<= 

Less than or equal to 

>=  

Greater than or equal to 

 What is the difference between: 

int i = 2;         // numeric value 2   
char a = ‘2’;      // equivalent to numeric value 50 
String s = ”2”;  // a Java (String) Object 
 

background image

R. Pełka:  Języki Programowania, 2008 

23 

Primitive types: 

boolean 

 boolean primitive type, names as for other                              

primitive types 

 type 

boolean 

 values: 

true false

 

 type conversion 

 no type conversion! 

 relational operators 

 apply on operands of 

numeric

 primitive data types 

 return 

boolean

 value 

 

 
 
 
 

 

2 < 4  , 4 <= 0  ,   3.14 > 9 

a < b 

a less than b? 

a <= b 

a less than or equal than b? 

a > b 

a greater than b? 

a >= b 

a greater than or equal to b?  

George Boole

  

the English mathematician (1815-1864)

  

background image

R. Pełka:  Języki Programowania, 2008 

24 

Operators 

 Equality Operators 

 Apply on 

operands of primitive data type 

 Return 

boolean

 value 

 

 

 

 

(2 == 4)   ,  (‘@’ != 6)  , (true == false) 

 

 Boolean Logical Operators 

 Apply on 

boolean

 

expressions 

 Return 

boolean

 value 

 

 

 

 

 (2 > 4) && (5 < 0),          !true || true 

 
 

 

Equality 

a == b 

Are a and b equal? (same primitive value?) 

Inequality 

a != b 

Are a and b not equal? (different primitive value?) 

OR 

AND 

true if either or both x, y are true, otherwise false 

x || y 

true if both x and y are true, false otherwise false 

x && y 

background image

R. Pełka:  Języki Programowania, 2008 

25 

Java operators 

– summary 

Arithmetic Op. :   

+   ++   -   --      *    /    %

                 

  Relational Op. :   

>   >=   <   <=   ==   != 

Logical Op. :   

&&  ||   ! 

Bit Op. :   

&  |  ^

(xor)

  ~

 (not)

   << 

(przes.)

 >>   >>>

 (z 0)      

Operators 

of Java

  

    Conditional Op. : 

 ? :      

(wyr_bool  

?

 

 

wart1 

wart2)  

 

Assign Op. :  

=  +=  -=   *=   /=  %=   &=   ^=   |=   >>= 

<<=   >>>=

    

Casting Op. :   

(Data Type) 

Array Op. :   

[] ([] [] , itd.)

 

Method Op. :   

()  .

    

instanceof Op. :  

instanceof 

 

operator instanceof określa, czy 

obiekt jest danego typu, np. obiekt instanceof typ (przy rzutowaniu) 

background image

R. Pełka:  Języki Programowania, 2008 

26 

Pierwszeństwo operatorów 

 Operator precedence 

 not    

 relational operators 

<   <=   >   >= 

 equality operators 

==    != 

 logical and   

&& 

 logical or      

|| 

 

Expression 

Evaluated as 

Result 

(4 == 2) && (1 < 4) 

(4 == 2) && (1 < 4) 

false 

!false || (2 >= 8) 

(!false)  ||  (2 >= 8) 

true 

false | false & true 

(false | (false & true)) 

false 

(4 == 2) == true 

((4 == 2) == true) 

false 

‘@’ != 6  

(‘@’ != 6 ) 

true 

Examples 
 

boolean b = (2 == 4); 
boolean c = (b & true); 

background image

R. Pełka:  Języki Programowania, 2008 

27 

Operators precedence 

                         Operator                                     Association   Precedence 

  ()  []  .   
   ! ~ ++ --  +  -   

(Data Type)  

   *   /   %  
   +   - 

 

   <<   >>   >>>     (Shift left, right, right unsigned) 

 

   <   <=   >   >=   instance 
   ==  !=   
   & 

 

   ^ 

 

   | 

 

   && 

 

   || 

 

   ? : 

 

   =  +=  -=  *=  /=  %=  &=  ^=  |=  <<=  >>=  >>>= 

Left Assoc.             (High)             
Left Assoc. 
Left Assoc. 
Left Assoc. 
Left Assoc. 
Left Assoc. 
Left Assoc. 
Left Assoc. 
Left Assoc. 
Left Assoc. 
Left Assoc. 
Left Assoc. 
Left Assoc. 
Left Assoc.             (Low) 

background image

R. Pełka:  Języki Programowania, 2008 

28 

Wrapper classes 

– klasy „opakowane” 

Sometimes it is necessary for a 

primitive 

type

 value to be an 

Object

, rather than 

just a primitive type. 

 

 Some data structures 

only store Objects

 Some Java methods 

only work on Objects

 

 Wrapper classes

 also contain some 

useful constants and a few handy 
methods. 

background image

R. Pełka:  Języki Programowania, 2008 

29 

Wrapper classes 

Each 

primitive type

 has an associated 

wrapper class

 

 

 

 

 

 

 

Each wrapper class 

Object

 can hold the value that would normally 

be contained in the primitive type variable, but now has a number of 

useful static methods

char 

Character 

int 

Integer 

long 

Long 

float 

Float 

double 

Double 

background image

R. Pełka:  Języki Programowania, 2008 

30 

Wrapper classes 

Integer number = new Integer(46);  

//”Wrapping” 

Integer num = new Integer(“908”); 
 
Integer.MAX_VALUE         

// gives maximum integer 

Integer.MIN_VALUE         

// gives minimum integer 

Integer.parseInt(“453”)   

// returns 453 

Integer.toString(653)     

// returns “653” 

 
 

int aNumber = number.intValue();   

// aNumber is 46 

background image

R. Pełka:  Języki Programowania, 2008 

31 

Constants 

 Things that do not vary 

 unlike variables 
 will never change 

 Syntax: 

final

 typeName variableName = value; 

 Example 

final

 int DELAY_MS = 120; 

 Constant names in all upper case 

 Java convention, not compiler/syntax requirement 

 Some predefined constants available: 

E, PI

  (java.lang.Math)