Download Data Structures and Algorithms - M Pervez Akhtar

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
By Mr. Muhammad Pervez Akhtar
[email protected]
http://pervez.farooka.com
 The
general environment
 The first program

Comments, Main, Terminal Output
 Primitive

Primitive types, Constants, Declaration and
Initialization, Terminal Input and Output
 Basics

types
Operators
Assignment Operators, Binary Arithmetic
Operators, Unary Operators, Type Conversion
Operators
 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
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
 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
 Comments
are non-compiled and nonexecutable 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
 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
 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

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:



::Decimal, Octal and Hexa
Character constant is enclosed with a pair of single
quotation marks, as in 'a‘


37,045,0x25
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"
 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
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 underscore 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
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
 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
 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
 The
IF, IF THAN ELSE statement
 Two
common mistakes
 Loops
the While, For, and Do statements
 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
 Switch

Suitable when multiple branches against one
Condition
 The


Statement
conditional operator
a shorthand for simple if-else statements
testExpr ? yesExpr : noExpr
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.
 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


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

Variables
Final Variable
If both static and final are used, they are global
symbolic constants