Download week2-oop - WordPress.com

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
Object Oriented Programming
Lecture #02
Zohaib Jan
Delving into Java basics
•
•
•
•
•
•
•
Types
Operators
Precedence
Code blocks
Keywords
Expressions
Statements
Primitive Types
•
•
•
•
•
•
•
•
Byte
Short
Int
Long
Float
Double
Char
Boolean
String
• Not a primitive data type
• Is an Object Type
• Unlike C Strings in Java is not a Char array /
pointer
• Demo of string
Operators in Java
•
•
•
•
•
DUH !....
Arithmetic Operators = +, -, /, *, %
Binary Operators = &, |
Binary Short cut Operators = &&, ||
Ternary Operator =
– String grade = cgpa > 3.0 ? “A” : “F” ;
Keywords, Statements ,and
Expressions
• Words that are reserved by Java
• 50 keywords
• Expressions
– Double meter = kilo * 1000;
More about instance variables
• Can you use instance variable without assigning
them values ?
–
–
–
–
–
–
–
–
–
For boolean: false
For byte: 0
For short: 0
For int: 0
For long: 0L
For float: 0.0f
For double: 0.0d
For char: \u0000 (the null character)
For Object: null
Code blocks
• {
//something
//something
//…..
• }
Scoping
• {
Variable here;
• }
• Cannot be accessed here;
Conditional logic
• if (something){
//do something
• }
• else if (something){
//do something else
• }
• else {
//otherwise
• }
Methods in Java
• returntype Methodname (arguments){
//codeblock
• }
Methods
• When to create methods ?
• Lets do some coding in order to clear this
Methods
• Repetitive tasks
Overloading
• Same method name
• Different method signature
Switch statement
• switch(condition){
case 1:
case 2:
case 3:
case 5: case 6: case 7:
default:
• }
Loops
• for (var; termination; increment){ }
• while (condition) {}
• do {} while (condition)
For each
• for ( String oneItemFromArray: someArray){
//do something
• }
Next up
• OOP with java