Download presentation
Presentation is loading. Please wait.
Published byJuliet Tate Modified over 8 years ago
1
CSE 110: Programming Language I Matin Saad Abdullah mabdullah@bracuniversity.net UB 1222
2
Comments Java programs can have three kinds of comments: Single-Line Comments // Block Comments General: /* */ Documentation Comments: /** */
3
Case Sensitive It simply means that the case of the letters in your Java programs matter. Java sees all of the following as different things: Level level LeveL level
4
Keywords In the Java programming language, a keyword is one of 50 reserved words which have a predefined meaning in the language.
5
Identifiers Identifiers are names Identifier may be any sequence of upper & lower case letters, numbers, or ‘_’, ‘$’ Cannot be Keywords They must not begin with a number Valid AcoountBalance index str1 $Test hello_world Invalid 2stryes/no low-high
6
Naming Conventions ThisIsSomeClass thisIsSomeMethod() thisIsSomeVariable some_variable preferred by some for private/local variables, NOT recommended CONSTANT_SOMETHING $someAutomaticallyGeneratedName
7
Literals A constant value in Java is created by using a literal representation of it. These are strings of symbols that represent “literal” data values. Integer Literals Like, 123 is an integer literal These are all decimal value (Base 10) Other two bases can be used – octal, hexadecimal Octal values are denoted by a leading ‘zero’, like – 0123 Hex values are denoted by leading ‘0x’ with numbers in the range of 0-15, [A through F are substituted for 10 to 15] Long literal are denoted by appending upper or lowercase ‘L’.
8
Literals Floating point literals expressed either in standard or scientific notation. Standard notation consists of decimal and fractional parts e.g. 12.0, 3.14159 Scientific notation uses decimal, fractional number plus a suffix (power of 10) e.g. 6.456E12, 12334e-5 Floating point literals in Java default to double precision To specify, a float literal, append a ‘F’ or ‘f’ to the constant.
9
Literals Boolean Literals only two logical values – true, false Difference with C, ‘true’ is not equal to ‘1’ and ‘false’ is not ‘0’ Character Literals includes Unicode character set represented inside a pair of single quote, like ‘d’, ‘@’ String Literals Enclosed between a pair of double quotes "123" is a String literal as is "class" but class is a keyword and Class is an identifier
10
Data types and variables Data types - simple to complex int - for integers or whole numbers double - for numbers with fractional parts String - for text Button - a button on a GUI Point - for representing points in a plane Variables store data in named locations every variable must have a declared type
11
Primitive types Java has eight primitive types: byte, short, int, long, float, double, char, boolean Primitive types have literal values and can be manipulated with built-in operators. E.g. 2 + 3
12
Eight Simple Data Types byte, short, char, int, long, float, and double char is 16 bit unsigned – Unicode, not ASCII (range 0 - 65535) fully international character set, represent all of the characters found in all human languages example: char ch = ‘X’ ;
13
Integer Types integer(no unsigned types) 8-bit byte 16-bit short 32-bit int 64-bit long
14
Ranges Name Width Range byte 8 -128 to 127 short 16 -32,768 to 32,767 float 32 3.4e-038 to 3.4e+038 double 64 1.7e-308 to 1.7e+308
15
Floating - Point Types Floating Point type float 32-bit 3.4 e-038 to 3.4 e+038 double 64-bit 1.7 e-308 to 1.7 e+308
16
Boolean Types standard type for only logical values has values true and false no conversion between ‘int’ and ‘boolean’
17
Declaring Variables All variables must be declared before can be used type identifier [= value] ; int count, total; String sentence; boolean done;
18
Initializing Variables Initializing is mandatory before accessing a variable int count = 10, total = 0; String sentence = "Hello there."; boolean done = false; Floating point literals in Java default to double
19
Initial Java Program 1.public class Test1 { 2. public static void main(String[] args) { 3. int x; 4. int y; 5. x = 2; 6. y = 3; 7. System.out.println (x + y); 8. } 9.}
20
1.// StringVsId.java 2.// contrast strings and identifiers 3.class StringVsId { 4. public static void main(String[] args) { 5. String hello = "Hello, world!"; 6. String stringVary; 7. stringVary = hello; 8. System.out.println(stringVary); 9. stringVary = "hello"; 10. System.out.println(stringVary); 11. } 12.} String AIRPO RT GULSH AN hellostringVa ry “Hello World” “hello” AIRPO RT GULSH AN
21
Scope of variables Java allows variables to be declared within a block A block defines Scope Each time stating a new block, new scope created Generally two types of scopes defined by ‘classes’ & ‘methods’ Variables declared inside a scope are not visible outside. Scope can be nested.
22
Scope of variables (cont..) 1.public class Scope { 2. public static void main(String[] args) { 3. int x; 4. x = 10; 5. { 6. int y = 20; // known only in this block 7. System.out.println (x + y); 8. } 9. y = 100; // ERROR !! y not known here 10. x = 200; // x is still known here 11. } 12.}
23
Scope of variables (cont..) public class Scope2 { public static void main(String[] args) { int x = 0; { int y = 100; System.out.println (x + y); y = 10; int y = 200; // Compile Error- already defined } int y ; // creates new variables ‘y’ }
24
Type Conversion Java’s automatic conversion occurs when - Two types are compatible e.g numeric types(int, float) are not compatible with ‘char’ or ‘boolean’ ‘char’ and ‘boolean’ are not compatible with each other. Destination type as larger than source type e.g. ‘int’ type is always large enough to hold ‘byte’ type This type of conversion is ‘widening conversion’
25
Incompatible Type Casting If it necessary to assign an ‘int’ to a ‘byte’ This conversion will not perform automatically, as ‘byte’ is smaller than ‘int’ This type of conversion is called ‘narrowing conversion’ A cast has general form (target-type) value
26
Type casting (example) byte b; int i = 257;// i = 257 (100000001) 2 double d = 323.142;// 323 (101000011) 2 b = (byte) i; System.out.println(b);// b = 1 i = (int) d; System.out.println(i);// i = 323 b = (byte) d; System.out.println(b);// b = 67 (01000011) 2
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.