Вы находитесь на странице: 1из 10

COMPUTER SCIENCE WITH JAVA

Java- It is an object oriented language developed by Sun Microsystems in 1991. Features of Java It is portable, i.e., it can run on any operating system and platform. In java big applications require less code. It is an open product, i.e., freely available to all, It offers built in graphics. Types of Java Programs- There are two types of java programs, Internet applets and Stand alone applications. Internet Applets are small programs that are embedded in web pages and are run on the viewers machine in a secured manner by Java capable browsers. Stand alone application is generally a software application that does not require low level operating system or hardware access. Java Byte Code- Java is a language which is both compiled and interpreted. Compiler converts source program into Java Byte Code which looks like machine language. Java Virtual Machine- Java interpreter is known as Java Virtual Machine (JVM). It translates java byte code into machine language. BlueJ- It is a Java Development Environment. It was designed by BlueJ team at Monash University, Melbourne. It comprises of an editor, a debugger and a viewer. Object- An object is an identifiable entity with some characteristics and behaviour. For example, a car, a mobile, etc. Object is also known as an instance. Class- Class is a blueprint or data type according to which objects of that type are created. For example, mobile is a class and Nokia6630 is an example of that class. Abstraction- Abstraction refers to the act of representing essential features without including the background details.

-1-

Encapsulation- The wr4apping up of data and functions (that operate on the data) into a single unit (called class) is called encapsulation. Inheritance- A class called sub-class inherits certain characteristics and behaviour from another class called super-class. This is termed as inheritance. Polymorphism- When different objects interpret a single message differently and respond it differently, it is known as polymorphism. Unicode- Unicode is a two byte character code set which represents almost all characters used by different languages of the world (65,536 characters). Java uses Unicode character set. Token- The smallest individual unit in a program is called token. Java has following token: keywords, identifiers, literals, punctuators and operators. Keywords- These are the reserved words and have special meaning. Identifiers- Identifiers are the names given to variables, objects, classes, functions, arrays, etc. Rules for naming identifiers: Alphabets, digits, underscore and $ can be used. Keywords cannot be used. Must not begin with a digit. Can be of any length. Java is case sensitive. Literals (constants)- These are values that are fixed. Java has following constants: integer constants- e.g., 12,-38976, etc. floating constants- e.g., 3.5F, 11.465D, 12E7, -0.132E-3, etc. boolean constants- e.g., true, false. character constants- e.g., w, &, 6, etc. String constants- e.g., India, computer, o, etc. Punctuators- These are special characters which act as separators in java. e.g., ( ), { }, [ ], ;, . Operators- These are symbols which represent various operations that are being carried on data. Unary operator- These operate on a single operand. e.g. +, -, ++, --. Binary operator- These operate on two operands. e.g., *, %, /, etc. There are following operators: Arithmetic operators: +, -, *, /, %.
-2-

Increment operator: ++ Decrement operator: -Relational operators: >, >=, <, <=, ==, != Logical operators: !, &&, || Assignment operators: = Shortcut assignment operators: +, -=, *=, /=, %= Ternary operator: ? :

Note: && and || operators compare the operands conditionally, i.e., if the first condition is true (in case of || operator) it will not check the second condition and will return true. In case of && operator if the first condition is false it returns false without checking the second condition. On the other hand & and | operators always check both the conditions. Expression- An expression is a combination of operators, constants and variables. Comment- A comment is a remark from the programmer which explains the program to the reader. Single line comment- // This is a single line comment. Multi line comment- /* This is a multi line comment. */ Documentation comment- They are used to produce HTML file. /** This is a documentation comment. */ Variable- A variable is a symbolic name of a memory location or a variable is a value that changes. Data Types- A data type is defined as the set of possible values a variable can hold. There are two categories of data types: Primitive datatypes- These are fundamental datatypes since they are not composed of any other datatypes. These are eight in number. 1. integers: They store integers, i.e., number without decimal part. byte (1 byte) short (2 bytes) int (4 bytes) long (8 bytes) 2. characters: They store a single character. char (2 bytes) 3. floating point numbers: They store numbers with decimal part. float (4 bytes) double (8 bytes) 4. boolean: They store true or false.
-3-

Reference datatypes- A reference in java is a data element whose value is an address. Arrays and classes are reference datatypes.

Dynamic Initialization- When a variable is initialization by the return value of a method call it is termed as dynamic initialization. Block- A block is a set of statements enclosed within curly brackets. Compound Statement- It is a set of one or more statements. Scope of a variable- The scope of a variable is defined as the area of program where the variable is visible (accessible). Variables declared within a method are local to that method only. Variables declared within a block are accessible only within that block. Lifetime of a variable- Lifetime of a variable is the duration for which a variable exists. Variables local to a method exist as long as the method executes. rvalue- Actual read value is the value stored in a variable. E.g. int a = 7; here 7 is the rvalue. lvalue- Location value is the memory address of the variable. Type conversion /casting- Process of converting one datatype to another is called type conversion. Implicit type conversion- Lower datatypes are automatically converted to higher datatype in expression whenever different datatypes are intermixed. This is also called type promotion. Explicit type conversion- Explicit (forced) conversion of one datatype to another is called explicit type casting. Syntax: (type)expression

if else statement- It is a conditional branch statement. If the condition evaluates to true, if part is executed otherwise else part is executed. switch statement- It is a multi-way branch selection statement, depending on the value of the expression, particular case statement is executed. default statement- If none of the constant matches the value of the expression in a switch statement, then the default statement is executed. The default statement is optional.

-4-

Differences between if-else and switchif-else switch Can evaluate any type of boolean Can only test for equality. expression. Floating point variables can be Floating point variables cannot used. be used. Less efficient but more versatile. More efficient but less versatile. Ternary operator- it requires three operands. General form is: expression1? expression2? expression3 expression1 is a condition. If it evaluates to true, expression2 is the result else expression3 is the result. for loop- It is a looping statement used to repeat statements a fixed number of times. General form is: for(initialization; condition; updation) { Statement; } while loop- It is a looping statement used to repeat statements as long as the condition remains true. It is used when it is not known how many times statements are to be repeat. It is also known as entry controlled loop. do while loop- It is a looping statement used to repeat statement as long as the condition remains true. It is used when it is not known how many times statements are to be repeated. It is also known as exit controlled loop. Differences between while and do while loopswhile do while Condition is checked before Condition is checked at the bottom entering the loop. of the loop. while loop may not execute even do while loop always executes at once. least once. break statement- It is a jump statement which causes the termination of switch statement, for loop, while loop and do while loop. continue statement- It causes the control to skip remaining statement of the loop and to proceed with the next iteration of the loop. Break and continue are generally used with if. Empty loop- An empty loop does not contain any statement in its loop body. E.g., for (int x =1; x <=10000; x ++);
-5-

Infinite loop- An infinite loop executes endlessly. It can be created by omitting condition. E.g., for (int x =1; ; x ++) { System.out.println(This is an endless loop); } Method/Function- A method is a subprogram which contains instructions to perform a specific task. Each method has a unique name and may or may not return a value. Formal Parameters- Variables defined within the parentheses that receive the values when the function is called are called formal parameters. Arguments- They are also called actual parameters. They are the values passed to a method when it is called. Pure Methods- They are also called Accessor methods. These methods do not change the state of the object. They just provide the information about the state of the object. Impure Methods- They are also called mutator methods. They change the state of the object. Function Prototype- It is the first line of the function definition. It gives information about the type of value returned by the function and type of parameter that function receives. Function signature- Signatures refers to the number and type of argument. Return statement- It is used in two ways: a) It causes an immediate exit from the function when it is encountered. b) It is used to return a value to the calling code. Static methods- These are also called class methods. These are not called through objects rather they are called through class itself. Call by value (pass by value)- In this method of function call, actual parameters are copied in the formal parameters hence actual and formal parameters are different. The changes made to the formal parameters are not reflected in the actual parameters. In java all primitive types are passed by value.

-6-

Call by reference- In this method of function call, the formal parameters become the references to the actual and formal refer to the same values. The changes are reflected in the original values. In java objects and arrays are passed by reference. Function overloading- Having more than one function with the same name which can be differentiated by the number and types of arguments is called function overloading. Function overloading implements polymorphism. Constructor- A member function with the class name is called constructor. It is used to initialize the objects of that class when they are created. Constructors have no return type, not even void. Default constructor- A constructor which receives no parameters is called default constructor. It is also called non parameterized constructor. If in a class no constructor is defined, Java supplies a default constructor which initializes instance variables with dummy values. Parameterized constructor- A constructor which receives parameters is called parameterized constructor. Constructor overloading- Having more than one constructor in a class is termed a constructor overloading. It is a feature of polymorphism. Package- A package is a group of logically related classes. E.g., java.io, java.lang, java.applet, java.util, java.net, etc. Instance variables- Variables defined within the class which represents attributes are called instance variables. They are also called data members and are created for every object of the class. Static variables- These are also called class variables. These are common for all the objects of the class. Access specifiers- These are access levels which control access to members of a class. There are four types of access specifiers: private- private members of a class are accessible only within the class. public- public members of a class are accessible from any class of any package. protected- protected members of a class are accessible from the class of the same package and all the subclasses. package/default/friendly- when no access specifier is used, access is friendly. All the classes of the same package can access such members.

-7-

new Operator- New is used to create objects and arrays. It is used to allocate memory for objects and arrays. this Operator - When an object calls a member function, the function automatically passes an in built argument which is a reference to the object that called the function. The reference is called this. Stream- Stream refers to the flow of data. Input stream- A source of input data is called an input stream. Output stream- Output data is called an output stream. Compile time errors- These errors are detected at the time of compilation. These arise because of improper syntax. Run time errors- Errors that occur at the time of execution are called run time errors. Exception- Run time errors are called exception. These are treated as objects in java. Wrapper classes- These are part of java.lang package. A wrapper class wraps a value of primitive type in an object. E.g., Character, Boolean, Byte, Short, Integer, Long, Float, Double. Exception handling- Exception handling refers to the way of handling exceptions by writing handlers (programs). try{ }- Lines of codes that are part of normal processing are placed within try block. catch{ }- Code to deal with an exceptions that might arise during the execution of the program is written within catch block. finally block- Code that must be executed whether an exception occurs or not is written within finally block. throws- Throws keyword is used to inform java compiler about the exception that might arise. After throws exception class name is given whose exception might arise. Array- An array is a finite set of data items stored in continuous memory locations referred to by a single name.
-8-

Subscript- Subscript refers to the position of the element within the array. It varies from 0 to number of elements-1. Single dimensional array- Here elements are referred by one subscript only. int [ ] a=new int [0];// creates an array with ten memory locations. Double dimensional array- Elements are referred by two subscripts. First subscript refers to row and second to columns. int [ ] [ ] a= new int [5] [3];// creates an array with 5rows and 3colums. length- It is a property of array used to find the number of elements in the array. arraycopy( )- This method is used to copy data from one array to another. System.arraycopy(source array, source index, destination array, destination index, no of elements to be copied). Linear searching- This is a method of searching an element in an array. Element to be searched is compared with every array element till it is found. It is time consuming if the array is large. Binary searching- This takes place in array arranged in ascending or descending order. The middle subscript of the element is calculated. The value to be searched is compared with middle positioned element. If value is less than middle element (assuming array is sorted in ascending order), search takes place in the first half of the array otherwise takes place in the second half of the array. The above process is repeated till the element is found if present. This process takes less time as the numbers of comparisons are reduced. Sorting- The process of arranging element in ascending or descending order is called sorting. Selection sorting- If the array is to be arranged in ascending order, the smallest element needs to be searched and exchanged with the first position element in the array. Again the smallest element from the remaining elements is selected and exchanged with second array element. This process continues till all elements are arranged. If the array needs to be arranged in the descending order, select the largest element. Bubble sorting- The adjacent elements of the array are compared. If array has to be arranged in ascending order and if a[0] is greater than a[1] they are

-9-

swapped(process of exchanging values is called swapping). This continues till elements are sorted.

-10-

Вам также может понравиться