Download presentation
Presentation is loading. Please wait.
1
By Mr. Muhammad Pervez Akhtar pervezbcs@gmail.com http://pervez.farooka.com
2
The general environment The first program Comments, Main, Terminal Output Primitive types Primitive types, Constants, Declaration and Initialization, Terminal Input and Output Basics Operators Assignment Operators, Binary Arithmetic Operators, Unary Operators, Type Conversion Operators
3
Conditional Statements Relational and equality operators, Logical operators, The if statement, The while statement, The for statement, The do statement, break and continue, The switch statement, The conditional operator Methods Overloading of method names Storage classes
4
Java Source code resides in files whose names end with the.java suffix The local compiler, javac, compiles the program and generates.class files, which contain bytecode Java bytecodes represent the portable intermediate language that is interpreted by running the Java interpreter, java. The interpreter is also known as the Virtual Machine Java programs, input can come from one of many places: Standard input, Command Line Arguments, A File, GUI Component
5
Save program as FirstProgram.java Compile program as javac FirstProgram.java Run program as javac FirstProgram Four things to note in program Comments, class, main method, standard output
6
Comments are non-compiled and non- executable code Three types of comments // : single line comment, inherited from C /*…*/: multi line comments inherited from c /**…*/: information for javadoc utility to generate documentation form comments Why Comments are needed? Make code easier for humans to read Helps programmers to modify the program Commented program is a good sign of programmer
7
Java program consists of interacting classes which contain methods and data elements Methods can be invoked using object of its class Static method can be invoked without object of its class Public static void main(String args[]) is special method with void return type, command line argument, public access modifier and is static It is the method that executes first of all
8
System.out.println: is the primary output mechanism in java Also called standard output (output on console) System.out.print: is another way of output println vs print println shifts control to start of next line after output but print does not shifts control to next line Can output integer, floating point, string, or some other type
10
Integer constants can be represented in either decimal, octal, or hexadecimal notation Octal notation is indicated by a leading 0; hexadecimal is indicated by a leading 0x or 0X Equivalent ways of representing the integer 37: 37,045,0x25 ::Decimal, Octal and Hexa Character constant is enclosed with a pair of single quotation marks, as in 'a‘ Internally, this character sequence is interpreted as a small number String constant consists of a sequence of characters enclosed within double quotation marks, as in "Hello"
11
Escape sequence is a constant but with special meanings It starts with a ‘\’ backslash followed by a character to print Followings are commonly used: ‘\n’New line character ‘\\’Backslash character ‘\’’Single quotation mark ‘\”’Double quotation mark
12
A primitive type, is declared by providing its type, its name, and optionally its initial value Type should be primitive type (int, float etc) The name must be an identifier, that consists of: Any combination of letters, digits, and the under- score character but it not start with a digit Reserved words are not allowed Not reuse identifier names that are already visibly used Are case sensitive, age or Age are different
13
Basic formatted terminal I/O is accomplished by nextLine and println The standard input stream is System.in The standard output stream is System.out Formatted I/O uses the String type + combines two Strings If one of the argument is not a String, a temporary String is created for it if it is a primitive type For input, we associate a Scanner object with System.in. Scanner has different methods for different primitive types
14
Operators are symbols to perform an operation Operators are used to form expressions Expression is combination of variables, constants and operators Following are the types of operators Assignment Operators =, +=, -=, *=, /= Binary Arithmetic Operators +, -, *, /, % Unary Operators ++, --, - ::prefix or postfix use Type Conversion Operators Generate a temporary entity of a new type
15
Statements that affect the flow of control Relational and Equality operators , >=, ==, != Logical Operators AND, OR, NOT or conjunction, disjunction and negation respectively &&, ||, ! Is another representation ! Has highest precedence and && has higher precedence than || && and || called short-circuit operators
16
The IF, IF THAN ELSE statement Two common mistakes Loops the While, For, and Do statements
17
Break and Continue statements Break statement terminate execution in the middle of a repeated statements (loop) Continue Statement Give up on the current iteration of a loop and go on to the next iteration
18
Switch Statement Suitable when multiple branches against one Condition The conditional operator a shorthand for simple if-else statements testExpr ? yesExpr : noExpr
19
Known as a function or procedure :: main() A method header consists of a name, a (possibly empty) list of parameters, and a return type The actual code to implement the method, sometimes called the method body A method declaration consists of a header plus the body Overloading of method Several methods may have the same name and be declared in the same class scope as long as their signatures(that is, their parameter list types) differ.
20
Local Variables: Entities that are declared inside the body of a method are local variables Can be accessed by name only within the method body Global Variables A variable declared outside the body of a method is global to the class if the word static is used, is similar to global variables in other languages Static Final Variable If both static and final are used, they are global symbolic constants
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.