[Go to site: main page, start]

0% found this document useful (0 votes)
3 views31 pages

Java Basics-DataTypes, Operators

The document provides an overview of variable declaration, numerical data types, and numeric operators in programming, particularly in Java. It explains data types, their ranges, and storage sizes, as well as arithmetic operations and type casting. Additionally, it covers programming style, including comments, naming conventions, and proper indentation.

Uploaded by

Usama Tayyab
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views31 pages

Java Basics-DataTypes, Operators

The document provides an overview of variable declaration, numerical data types, and numeric operators in programming, particularly in Java. It explains data types, their ranges, and storage sizes, as well as arithmetic operations and type casting. Additionally, it covers programming style, including comments, naming conventions, and proper indentation.

Uploaded by

Usama Tayyab
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Variable Declaration

▪ Name

▪ Data type

▪ Initialize with value

double radius = 2.0;


Numerical Data Types
Name Range Storage Size

byte –27 (-128) to 27–1 (127) 8-bit signed

short –215 (-32768) to 215–1 (32767) 16-bit signed

int –231 (-2147483648) to 231–1 (2147483647) 32-bit signed

long –263 to 263–1 64-bit signed


(i.e., -9223372036854775808
to 9223372036854775807)
float Negative range: 32-bit IEEE 754
-3.4028235E+38 to -1.4E-45
Positive range:
1.4E-45 to 3.4028235E+38
double Negative range: 64-bit IEEE 754
-1.7976931348623157E+308 to
-4.9E-324
Positive range:
4.9E-324 to 1.7976931348623157E+308
Numeric Operators

Name Meaning Example Result

+ Addition 34 + 1 35

- Subtraction 34.0 – 0.1 33.9

* Multiplication 300 * 30 9000

/ Division 1.0 / 2.0 0.5

% Remainder 20 % 3 2
Integer Division

+, -, *, /, and %

5 / 2 yields an integer 2.
5.0 / 2 yields a double value 2.5

5 % 2 yields 1 (the remainder of the division)


Remainder Operator
Remainder is very useful in programming. For
example:

- Determine whether a number is even or odd.

- Determine position within a cycle


Saturday is the 6th day in a week
A week has 7 days
(6 + 10) % 7 is 2
The 2nd day in a week is Tuesday
After 10 days
Floating-Point Numbers are
Approximated
Calculations involving floating-point
numbers are approximated because these
numbers are not stored with complete
accuracy. For example,
[Link](1.0 - 0.1 - 0.1 - 0.1 - 0.1 -
0.1);
displays 0.5000000000000001, not 0.5, and
[Link](1.0 - 0.9);
displays 0.09999999999999998, not 0.1.
Number Literals
A number literal is a notation that we use to
represent values

int i = 34;
long x = 1000000;
double d = 5.5;
Default Numeric Data Type
▪ In Java, an integer (e.g. 1 or
2,147,483,647) literal is by default a type
int.

▪ Floating-point literals are written with a


decimal point (e.g. 5.0,
9800000000000.34). By default, a floating-
point literal is treated as a type double.
Choosing Other then Default Data
Type
One can choose a data type other then the
defaults to hold values represented by
their number literals:
- 5L will force Java to store the value of 5
using long (64bit) instead of int (32bit)
-5.0F will force Java to store the value
represented by the literal 5.0 using float
(32bit) instead of double (64bit)
Scientific Notation
Floating-point literals can also be specified in
scientific notation, for example, 1.23456e+2,
same as 1.23456e2, is equivalent to
123.456, and 1.23456e-2 is equivalent to
0.0123456. E (or e) represents an exponent
and it can be either in lowercase or
uppercase.
Arithmetic Expressions
3 + 4 x 10( y − 5)( a + b + c) 4 9+ x
− + 9( + )
5 x x y

is translated to

(3+4*x)/5 – 10*(y-5)*(a+b+c)/x + 9*(4/x + (9+x)/y)


Shortcut Assignment
Operators
Operator Example Equivalent
+= i += 8 i = i + 8
-= f -= 8.0 f = f - 8.0
*= i *= 8 i = i * 8
/= i /= 8 i = i / 8
%= i %= 8 i = i % 8
Increment and
Decrement Operators
Operator Name Description
++var preincrement The expression (++var) increments var by 1 and evaluates
to the new value in var after the increment.
var++ postincrement The expression (var++) evaluates to the original value
in var and increments var by 1.
--var predecrement The expression (--var) decrements var by 1 and evaluates
to the new value in var after the decrement.
var-- postdecrement The expression (var--) evaluates to the original value
in var and decrements var by 1.
Increment and
Decrement Operators, cont.

int i = 10; Same effect as


int newNum = 10 * i++; int newNum = 10 * i;
i = i + 1;

int i = 10; Same effect as


int newNum = 10 * (++i); i = i + 1;
int newNum = 10 * i;
Numeric Type Conversion
Consider the following expressions:

25/4

25/4.0+455*8
Conversion Rules
When performing a binary operation involving
two operands of different types, Java
automatically converts the operand based on the
following rules:

1. If one of the operands is double, the other is


converted into double.
2. Otherwise, if one of the operands is float, the
other is converted into float.
3. Otherwise, if one of the operands is long, the
other is converted into long.
4. Otherwise, both operands are converted into int.
Operator-Operand Defined
▪ x = 10 * 16

▪ Variable = operand_1 operator operand_2


Type Casting
Implicit casting
double d = 3; (type widening)

Explicit casting
int i = (int)3.0; (type narrowing)
int i = (int)3.9; (Fraction part is
truncated)
What is wrong? int x = 5 / 2.0;

range increases

byte, short, int, long, float, double


Character Data Type
char letter = 'A'; (ASCII)
char numChar = '4'; (ASCII)

NOTE: The increment and decrement operators can also


be used on char variables to get the next or preceding
Unicode character. For example, the following statements
display character b.
char ch = 'a';
[Link](++ch);
Escape Sequences for Special
Characters
Description Escape Sequence Unicode
Backspace \b \u0008
Tab \t \u0009
New line \n \u000A
Carriage return \r \u000D
Backslash \\ \u005C
Single Quote \' \u0027
Double Quote \" \u0022
Appendix B: ASCII Character Set
ASCII Character Set is a subset of the Unicode from \u0000 to \u007f
Casting between char and
Numeric Types
int i = 'a'; // Same as int i = (int)'a';

char c = 97; // Same as char c = (char)97;


Outputting to Screen
double radius = 2.0;
▪ Text strings in “” are printed exactly
▪ [Link](“radius”); => radius

▪ Text strings without “” are interpreted as variable


identifiers
▪ [Link]( radius ); => 2.0

▪ + sign is used to combined together text string output


with variable value
▪ [Link](“Length of radius is: “+radius+” in.”);
▪ Length of radius is : 2.0 in.
Programming Style and
Documentation
▪ Appropriate Comments
▪ Naming Conventions
▪ Proper Indentation and Spacing
Lines
▪ Block Styles
Appropriate Comments
Summary of:
-Program’s function
-Algorithm used for implementation
-Type of input
-Type of output if any
Naming Conventions
▪ Choose meaningful and descriptive
names.
▪ Variables and method names:
▪ Use lowercase. If the name consists of
several words, concatenate all in one, use
lowercase for the first word, and capitalize
the first letter of each subsequent word in
the name. For example, the variables
radius and area, and the method
computeArea.
Naming Conventions, cont.
▪ Class names:
▪ Capitalize the first letter of each word
in the name. For example, the class
name ComputeArea.

▪ Constants:
▪ Capitalize all letters in constants, and
use underscores to connect words.
For example, the constant PI and
MAX_VALUE
Proper Indentation and Spacing
▪ Indentation
▪ Indent two spaces.

▪ Spacing
▪ Use blank line to separate segments of the
code.
public class ComputeArea {
public static void main(String[] args) {
final static double PI = 3.14159;
double radius;
double area;
radius = 20;
area = radius * radius * PI;
[Link]("The area for the circle of
radius " + radius + " is " + area);
}
}
public class ComputeArea {

/** Main method **/


public static void main(String[] args) {
final static double PI = 3.14159;
double radius;
double area;

// Assign a radius
radius = 20;

// Compute area
area = radius * radius * PI;

// Display results
[Link]("The area for the circle of radius " +
radius + " is " + area);
}

}
Misc.
▪ Words are case sensitive
▪ All statements must be closed with a
semicolon “;”

You might also like