100% found this document useful (3 votes)
9K views10 pages

Java Basics for Class 11 Computer Science

- Java is an object-oriented programming language developed by Sun Microsystems in 1991. It is portable, requires less code for big applications, and offers built-in graphics. - There are two types of Java programs: applets for web pages and standalone applications. Java code is compiled into bytecode, which is run on the Java Virtual Machine (JVM). - Key concepts in Java include classes and objects, inheritance, polymorphism, and encapsulation. The language uses object-oriented programming features like classes, objects, methods, and interfaces.

Uploaded by

Varun Dhawan
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
100% found this document useful (3 votes)
9K views10 pages

Java Basics for Class 11 Computer Science

- Java is an object-oriented programming language developed by Sun Microsystems in 1991. It is portable, requires less code for big applications, and offers built-in graphics. - There are two types of Java programs: applets for web pages and standalone applications. Java code is compiled into bytecode, which is run on the Java Virtual Machine (JVM). - Key concepts in Java include classes and objects, inheritance, polymorphism, and encapsulation. The language uses object-oriented programming features like classes, objects, methods, and interfaces.

Uploaded by

Varun Dhawan
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd

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 ++) { [Link](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., [Link], [Link], [Link], [Link], [Link], 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 [Link] 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. [Link](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-

Common questions

Powered by AI

Dynamic initialization in Java refers to the practice of initializing a variable at runtime through the return value of a method call. This approach is advantageous because it allows variable initialization to be determined by computations or conditions that are only known at runtime, providing flexibility. For example, a variable that holds the result of a complex calculation can be dynamically initialized with the actual result of a method that performs the computation, thereby ensuring that the variable holds the most current and accurate value based on the latest context .

Method overloading in Java occurs when multiple methods in the same class have the same name but different parameter lists. It allows the same method to operate on different types, promoting code reusability. In contrast, method overriding happens when a subclass provides a specific implementation of a method already defined in its superclass. Both overloading and overriding support polymorphism, a key concept in object-oriented programming, which allows objects to be treated as instances of their parent class. This enables a single interface to perform different functions based on the object’s actual instance during runtime .

Exception handling in Java involves managing runtime errors to maintain normal program flow. A try block contains code that might throw an exception. If an exception is thrown, execution immediately passes to the catch block, which handles the specific type of exception. The finally block, if present, executes after try-catch regardless of whether an exception occurred, allowing for cleanup operations. The throws keyword is used in method signatures to declare potential exceptions that the method might throw, enabling propagation of the exception up the call stack for handling by the calling method .

Java's syntax is enriched by keywords, literals, and operators that define its structure and functionality. Keywords, such as 'class', 'public', and 'if', serve as reserved terms that have special meanings and uses in the language. Literals represent fixed values within the code, such as integer literals (e.g., 42), boolean literals (e.g., true, false), and string literals (e.g., "Hello"). Operators, such as +, -, * (arithmetic operators), and >=, ==, != (relational operators), perform operations on variables and constants. Together, they form expressions, control structures, and object manipulations that constitute a Java program .

In Java, the for loop is used to execute a block of code a specific number of times with syntax specifying initialization, condition, and update expressions. In contrast, the while loop evaluates its condition before executing the loop body, making it ideal for scenarios where the number of iterations is unknown upfront. The do-while loop, unlike the while loop, checks its condition after executing the loop body, ensuring that the block of code runs at least once. Thus, each loop type serves different programming requirements based on the certainty of iteration counts .

A constructor in Java is a special method used to initialize objects. It has the same name as the class and no return type. Constructor overloading, where multiple constructors are defined with different parameter lists, implements polymorphism by allowing objects to be instantiated in various ways. This flexibility enables the creation of objects with different initial states or attributes, depending on the requirements, thus supporting diverse object constructions within the same class framework .

Access specifiers in Java define the visibility of class members, impacting both code security and structure by controlling how and where members can be accessed. The 'private' specifier restricts access to the class itself, enhancing encapsulation. 'Public' grants wide accessibility, allowing cross-package and cross-class access. 'Protected' allows access within the same package and subclasses, promoting inheritance. Lastly, default (package-private) access is limited to the package, offering a balance between exposure and encapsulation. Carefully selecting access specifiers helps maintain a secure and well-organized codebase .

Encapsulation in Java enhances data security by restricting direct access to some components of an object. This is achieved by declaring class variables as private and providing public getter and setter methods to access and modify them. For instance, in a banking application, account balance details can be kept private inside an Account class, and public methods like getBalance() and deposit(double amount) are exposed to manipulate and retrieve the balance amount. By controlling access, encapsulation protects the data from unauthorized access and modifications, thus enhancing security .

Abstraction in Java is the concept of hiding complex implementation details and showing only the essential features of an object. This is achieved through the use of abstract classes and interfaces that define a template or a contract for concrete classes to implement. For example, in a software application for a car rental service, an abstract class called 'Vehicle' might define abstract methods like startEngine(), stopEngine(), and accelerate(). Various classes like 'Car', 'Truck', and 'SUV' would then implement these methods according to their specific details, allowing the application to handle different types of vehicles in a uniform manner .

Java achieves platform independence through the use of Java Byte Code, which is an intermediate representation of Java programs. When a Java program is compiled, it is converted from source code into Java Byte Code by the Java compiler. This Byte Code is then interpreted by the Java Virtual Machine (JVM), which translates it to machine code that is understandable by the host operating system. This abstraction provided by the JVM allows Java applications to run on any platform that has a compatible JVM implementation, ensuring platform independence .

COMPUTER SCIENCE WITH JAVA
Java- It is an object oriented language developed by Sun Microsystems in 
1991.
Features of Java-
Encapsulation- The wr4apping up of data and functions (that operate on 
the data) into a single unit (called class) is called
•
Increment operator: ++
•
Decrement operator: --
•
Relational operators: >, >=, <, <=, ==, !=
•
Logical operators: !, &&, ||
•
Reference datatypes- A reference in java is a data element whose value 
is an address. Arrays and classes are reference dat
Differences between if-else and switch-
if-else
switch
•
Can evaluate any type of boolean
      expression. 
•
Floating point
Infinite loop- An infinite loop executes endlessly. It can be created by 
omitting condition.
E.g., for (int x =1;  ; x ++)
{
Call by reference- In this method of function call, the formal parameters 
become the references to the actual and formal ref
new Operator- New is used to create objects and arrays. It is used to allocate 
memory for objects and arrays.
this Operator
Subscript- Subscript refers to the position of the element within the array. It 
varies from 0 to number of elements-1.
Singl
swapped(process of exchanging values is called swapping). This continues till 
elements are sorted.
-10-

You might also like