0% found this document useful (0 votes)
17 views45 pages

Chapter 7

Computer important points for class 12 chapter 7 for boards

Uploaded by

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

Chapter 7

Computer important points for class 12 chapter 7 for boards

Uploaded by

princeyadav97654
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
You are on page 1/ 45
Introduction to Java ‘Tova is object-oriented programming language developed by Sun Microsystems in 1991 ‘The jve languoge was designed to be small, simple ond portable across platforms ond operating systems, both at the source and at the binary level ‘Java is small, fast, efficient and easily portable to a wide range of hardware devices Tt is considered as one of the ideal language for distributing executable programs via the World Wide Web and also a general-purpose programming language for developing programs that ore is used as a placeholder that describes something actual we need ‘o type while writing actual program, ‘The definition of the method in Java consists of function header and the sequence of statement ‘enclosed between braces {and}. « in the first lin is the name of the class having main methed in it. Variable and method declaration after ond before main() method is optional. Data Types Data type determines the required memory size, type of volues, range of values and type of ‘operations that can be performed. ‘Java supports eight primitive data types. ‘The primitive data types are named byte, short, int, long, float, double, char, Boolean, ‘The first four typed hold integers, next two hold real numbers, char holds a single character ond Variable Lf anything is to be remembered by the computer, there will be « requirement of veriables during program execution, Tt needs to be stored in the memery of the computer. Programs manipulate the data that are stored in memery. ‘Tn machine language, data can only be referred to by giving the numerical eddress of the location in memory where itis stored. In a high-level language such os Jova, names are used instead of numeric address of memory location to refer to date. “The programmer hes to remember only the nome. ‘Arname is used to refer to the data stored in memory and it i called « variable, ‘A variable can be used ina java program only is it has been declared, ‘One or more variables can be declared in Java using declaration statement with following syntax. {variable-names); ‘ANKIT GOHIL + The conventions used here in the syntax are a follows ¥ Angle brackets <> denote the item to be specified by user Curly brackets { } denote the list of items separated by commas. + 2 «expression!» : pein Recor em stent i octet > Assignment : ‘In Java, an expression containing assignment (=) operator is generally referred to os assignment stotement. ‘Once a variable has been declared, a value can be assigned to that variable by using the assignment ‘operator ‘In Java, one of the ways to get data into a variable is with an assignment statement, takes: Shorthand ossignment operators. ‘Tava also support shorthand version of assignment, Tt saves the typing time, Tt takes the form «variable» operator» = cexpression. Here the operator should be a binary operator using two operands Type cast ‘+ Insome cases, the programmer may wont to force a conversion that wouldrit be done automatically ‘+ For this a type cast can be used ‘+A type cast is indicated by putting a type name in parentheses before the value we want to convert ‘+ Thus it takes a form : («dota-type>) «expression + For example : into: short b: b= (short) a: + Here, variable ais explicitly converted using type cost too value of type short. Y Precedence and associativity of Java operators: ‘+ Tava has well defined rules for specifying the order in which the operators in an expression are. ‘eveluated when the expression has severel operators. ‘+ The operators are evaluated es per their privity or precedence. ANKIT GOHIL ¥ Precedence Order : When two operators are having different privity, then an operator with the higher precedence is operated first, For example, in expression 2+ b* c, multiplication is having higher precedence then addition, so b* € is evaluated first and then the result is edded to 0, Precedence rules can be overridden by explicit parentheses, So, instead of creating confusion, use of ¥ Associativity : ‘+ When two operators with the some precedence appear in the expression, the expression is ‘evaluated according to its associativity. ‘+ Associativity determines the direction (left-to-right or right-to-left) in which operations are performed. Control Structures ‘In general, the statements are executed sequentially, one by one. ‘Sometimes, program logic needs to change the flow of this sequence. ‘This statements that enable to control the flow of execution are considered as control structures, ‘There are two types of control structures: loops and branches, Loops are used to repeat a sequence of statement over and over until some condition occurs. + Bre Block A block statement isa group of statement enclosed between a pair of braces, “(and ‘The format of a block is C ) Block can be used for various purposes as follows : 4 To group a sequence of statements into a unit that is to be treated as o single statement, usually in control structures. 4 To group logically related statements, 4 To create voriables with local scope for statement within a block. ‘When we declare variables inside a block, they are local in that block and such variables will cease ‘to exist after the block. ‘A ariable declared inside a block is completely inaccessible and invisible form outside that block. stotements> ANKIT GOHIL When the variable declaration statement is executed, memory is allocated to hold the value of the voriable, ‘When the block ends, that memory is released and is made available for reuse. ‘The variable is said to be local to the block. ‘There is @ generel concept called the “scope” of a variable, ‘The scope of a variable is the part of the program in which that variable is valid. if Statement ‘+ The if statement when used ina program enables to toke one of two alternative courses of action, depending on whether the velue of « given Boolean-valued expression is true or false. ‘+ Tris an example of a "branching" or “decision’ or “selective” control structure, below: When if statement is executed, it first evaluates the Boolean expression TF its volue is true, it executes statement-t; otherwise it executes a block of statements written, ofter keyword else, in if statement is usually a block statement, T+ may be any single statement, but it is advisable to use block to make it easy to insert other statement later. ‘When an if statement is used in another if statement, itis called nested-if statement. ‘+The form of switch statement is shown switch (cexpression») ( In the switch statement, the test expression should be of the type byte, char, short er int. It con also be of enum dota type. ‘When executing switch statement, the volue of the test expression is compared with each of the ‘case values in turn from casel onwards, ‘Tf a match is found, the respective case statements ore executed. Tf no match is found, the default statement is executed, Repetitive Control Structures ‘Tava supports three types of looping constructs : for, while ond do..while In for and while loop, test expression is evaluated first ond the statements in the loop are ‘executed if condition is true, ‘These loops are entry-controlled or pre-test loop constructs, ‘In do..while loop, it evaluates the test expression after executing the statements in loop. Tt repeats the loop if the test condition is true. ‘Thus, do.hle loop is exit controlled or post-test loop construct, Here, statements of loop are executed atleast once, The control is transferred at the first statement ofter the end of loop structure. Break jumps outside the nearest loop containing this statement. Use of continue statement is used to skip the following statements in a loop and continue with the next iteration Labelled loops and labelled break : When there is @ use of nested loops, break statement breaks the neorest enclosing loop and ‘transfers the control outside the loop. ‘Similarly continue olso restarts the enclesing oop. Tf the programmer wants to control which loop to break and which loop to reiterate, labeled loop con be used. ‘To use « labelled loop, add the label followed by colon (:) before the loop. ‘Then add the name of the label after the keyword break or continue to transfer control elsewhere ‘other than enclosing loop. ANKIT GOHIL

You might also like