



















Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
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
1 / 27
This page cannot be seen from the preview
Don't miss anything!




















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
execution
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 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 "
System.out.println( "The number of days in a non-leap year is "
// PI = 3.14;// The statement above would generate a compiler error// You cannot change the value of a constant} }
Advantages
errors^ – An example?
Operator Precedence
-^
Operator Precedence
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
Operator
Operation
addition
-^
subtraction
multiplication
/^
division
%
modulus(remainder afterdivision)
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
=
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;
Translate
x
into Java:
2y
// incorrect! double result = x / 2 * y;double result = x / 2 * y;^ => x
y
// correctdouble result = x / ( 2 * y );
Example:
int result = 4 / 0;
generates
ArithmeticException
and program
stops executingstops executing
Infinity
a
(not a number)
operands of different data types:^ – Lower-precision operands are promoted to
higher-precision data types, then the operation is performedis performed
evaluation; not a permanent change
floating-point operand will have a floating-point result.