Java Programming Basics: Compatible Data Types, Constants, and Arithmetic Operators - Prof, Study notes of Javascript programming

An introduction to java programming basics, covering compatible data types, constants, and arithmetic operators. It includes examples and explanations of various concepts, such as data type compatibility, constant declaration, and arithmetic operations.

Typology: Study notes

Pre 2010

Uploaded on 09/17/2009

koofers-user-2o9
koofers-user-2o9 🇺🇸

4.5

(2)

10 documents

1 / 27

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Chapter 2
Programming Building Blocks
Java Basics
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b

Partial preview of the text

Download Java Programming Basics: Compatible Data Types, Constants, and Arithmetic Operators - Prof and more Study notes Javascript programming in PDF only on Docsity!

Chapter 2

Programming Building Blocks

— Java Basics

Compatible Data Types

Any type in right column can be assigned to type in left

column: Data Type

Compatible Data Types

byte

byte

short

byte, short

int

byte, short, int, char

long

byte, short, int, long, char

float

float, byte, short, int, long, char

double

float, double, byte, short, int, long, char

boolean

boolean

char

char

otes

-^

The keyword static allows main( ) to be calledwithout having to instantiate a particular instance ofthe class. This is necessary since main ( ) is calledby the Java interpreter before any objects are made.

-^

Java compiler will compile classes that do notcontain a main ( ) but the Java interpreter has noway to run these classes.

-^

Any information that you need to pass to a methodis received by variables specified within the set ofparenthesis. In main ( ) there is only one parameter.String args[ ]declares a parameter named args whichis a array of instances of the class String. Here, argsreceives any command line arguments present whenthe program is executed.

String Literals

Escape Sequences

-^

To include a special character in a

String

, use

an escape sequence Character

Escape Sequence

Newline

\n

Tab

\t

Tab

\t

Double quotes

"

Single quote

'

Backslash

\

Backspace

\b

Carriage return

\r

Form feed

\f

Constants

  • Value cannot change during program

execution

  • Syntax: final

dataType constantIdentifier =

assignedValue;

ote

: assigning a value when the constant is

declared is optional. But a value must be assignedbefore the constant is used.

Constants.java

/* Constants Class*/ public class Constants{ public static void main( String [] args ){

final char ZORRO = 'Z';final double PI = 3.14159; final int DAYS_I0_LEAP_YEAR = 366, DAYS_I0_0O0_LEAP_YEAR = 365;final int DAYS_I0_LEAP_YEAR = 366, DAYS_I0_0O0_LEAP_YEAR = 365; System.out.println( "The value of constant ZORRO is " + ZORRO );System.out.println( "The value of constant PI is " + PI );System.out.println( "The number of days in a leap year is "

  • DAYS_I0_LEAP_YEAR );

System.out.println( "The number of days in a non-leap year is "

  • DAYS_I0_0O0_LEAP_YEAR );

// PI = 3.14;// The statement above would generate a compiler error// You cannot change the value of a constant} }

Advantages

  • Constants make the code more readable
    • PI is more meaningful than 3.
      • Prevent programmers from making logical

errors^ – An example?

Expressions and Arithmetic

Operators

  • The Assignment Operator and Expressions• Arithmetic Operators •^

Operator Precedence

-^

Operator Precedence

  • Integer Division and Modulus• Division by Zero• Mixed-Type Arithmetic and Type Casting• Shortcut Operators

Examples:

int numPlayers = 10; // numPlayers holds 10numPlayers = 8;

// numPlayers now holds 8

int legalAge = 18; int voterAge = legalAge;int voterAge = legalAge; The next statement is illegal int height = weight * 2; // weight is not definedint weight = 20; and generates the following compiler error:

illegal forward reference

Arithmetic Operators

Operator

Operation

addition

-^

subtraction

multiplication

/^

division

%

modulus(remainder afterdivision)

Example

You have 2 quarters, 3 dimes, and 2 nickels.How many pennies are these coins worth? int pennies = 2 * 25 + 3 * 10 + 2 * 5;

=

50

30

  • 10

=

90

=

90

Can also be written as :int pennies = (225) + (310) + (2*5);If you want to change the order of evaluation:int pennies = 2 * (25 + 3) * (10 + 2) * 5;

Another Example

Translate

x

into Java:

2y

// incorrect! double result = x / 2 * y;double result = x / 2 * y;^ => x

y

// correctdouble result = x / ( 2 * y );

Division by Zero

  • Integer division by 0:

Example:

int result = 4 / 0;

  • No compiler error, but at run time, JVM

generates

ArithmeticException

and program

stops executingstops executing

  • Floating-point division by 0:
    • If dividend is not 0, the result is

Infinity

  • If dividend and divisor are both 0, the result is

a

(not a number)

Mixed-Type Arithmetic

  • When performing calculations with

operands of different data types:^ – Lower-precision operands are promoted to

higher-precision data types, then the operation is performedis performed

  • Promotion is effective only for expression

evaluation; not a permanent change

  • Called "implicit type casting"
    • Bottom line: any expression involving a

floating-point operand will have a floating-point result.