×
Push yourself because, no one else is going to do it for you.
--Your friends at LectureNotes
Close

Note for Core Java - cjava by Ashwini S

  • Core Java - cjava
  • Note
  • Computer Science Engineering
  • B.Tech
  • 0 Views
  • Uploaded 2 months ago
Ashwini S
Ashwini S
0 User(s)

Share it with your friends

Leave your Comments

Text from page-1

NOTES PREPARED BY ASHWINI KHADYE (B.E.- Computer) Contact : 9920139393 / 7718059393 TOPIC-1: INTRODUCTION TO JAVA 1. What is JVM? What is byte code? (S-15) (JVM- 3 M, bytecode- 1 M) Ans: - JVM is the Java Virtual Machine - JVM exists only in the computer memory - It is a simulated computer within the computer and does all the major functions of a real computer - The java compiler compiles java program and produces an intermediate code known as byte code for the java virtual machine. Java Compiler Source Code Java Program Byte Code Virtual Machine Process of Compilation -- Byte Code (Virtual machine code) is not machine specific Machine specific code is generated by Java Interpreter by acting as an intermediary between the virtual machine and the real machine. Interpreter is written for each type of machine. Byte Code Virtual Machine Java Interpreter Machine Code Real Machine Process of converting byte code into machine code  - Byte code: Bytecode is the compiled format for Java programs. Once a Java program has been converted to bytecode, it can be transferred across a network and executed by Java Virtual Machine (JVM). A Bytecode file generally has a .class extension. 2. Explain any four features of Java. (Any four features - 1 Mark each) Ans: 1. Compile & Interpreted: Java is a two staged system. It combines both approaches. First java compiler translates source code into byte code instruction. Byte codes are not machine instructions. In the second stage java interpreter generates machine code that can be directly executed by machine. Thus java is both compile and interpreted language. 2. Platform independent and portable: Java programs are portable i.e. it can be easily moved from one computer system to another. Changes in OS, Processor, system resources won’t force any change in java programs. Java compiler Page 1 of 10

Text from page-2

NOTES PREPARED BY ASHWINI KHADYE (B.E.- Computer) Contact : 9920139393 / 7718059393 generates byte code instructions that can be implemented on any machine as well as the size of primitive data type is machine independent. 3. Object Oriented: Almost everything in java is in the form of object. All program codes and data reside within objects and classes. Similar to other OOP languages, java also has basic OOP properties such as encapsulation, polymorphism, data abstraction, inheritance etc. Java comes with an extensive set of classes (default) in packages. 4. Robust & Secure: Java is a robust in the sense that it provides many safeguards to ensure reliable codes. Java incorporates concept of exception handling which captures errors and eliminates any risk of crashing the system. Java system not only verify all memory access but also ensure that no viruses are communicated with an applet. It does not use pointers by which you can gain access to memory locations without proper authorization. 5. Distributed: It is designed as a distributed language for creating applications on network. It has ability to share both data and program. Java application can open and access remote object on internet as easily as they can do in local system. 6. Multithreaded: It can handle multiple tasks simultaneously. Java makes this possible with the feature of multithreading. This means that we need not wait for the application to finish one task before beginning other. 7. Dynamic and Extensible: Java is capable of dynamically linking new class library’s method and object. Java program supports function written in other languages such as C, C++ which are called as native methods. Native methods are linked dynamically at run time. 3. Define wrapper class. Give the following wrapper class methods with syntax and use: a. To convert integer number to string. b. To convert numeric string to integer number. c. To convert object numbers to primitive numbers using typevalue() method. (Wrapper class – 2 M, each method syntax and use – 2 M each) Ans: Wrapper Class: - Objects like vector cannot handle primitive data types like int, float, long char and double. - Wrapper classes are used to convert primitive data types into object types. - Wrapper classes are contained in the java.lang package. The some of the wrapper classes are: Simple type Wrapper class Boolean Int Char Float Boolean Integer Character Float Page 2 of 10

Text from page-3

NOTES PREPARED BY ASHWINI KHADYE (B.E.- Computer) Contact : 9920139393 / 7718059393 a) To convert integer number to string : Method is toString() String str = Integer.toString(i) – converts the integer i to string value. b) To convert numeric string to integer number: Method is parseInt() int i = Integer.parseInt(str) – converts the string value of numeric string str to int i. c) To convert object number to primitive number using typevalue() method - The method here is typeValue(), meaning that type can be the data type of the number. - For example : if x is an Integer object with value 10, then it can be stored as primitive int y with the method intValue() as Integer x = new Integer(10); int y = x.intValue(); - Similarly, it can be done with other numeric types as floatValue(), doubleValue() etc. 4. ‘?:’ what this operator is called? Explain with suitable example. (Name of the operator – 1M, Explanation and example – 3 M) Ans: - ‘?:’ is called as conditional operator or ternary operator. - It can be used to check condition in one line, instead of writing if…else statement. - Its format is (condtion ? true case : false case) - If condition is true, then true case will be executed otherwise false case will be execute. - Example: int a,b; a = 10; b = ( a > 5 ? 12 : 20 ); Here b = 12 as a is 10 and condition is true. If value of a is changed to 15 then b will have value as 20. 5. Define JDK. List the tools available in JDK explain any one in detail. (Definition 1 M, list any four tools -2M, explanation of any component -1 M) - Definition: A Java Development Kit (JDK) is a collection of tools which are used for developing, designing, debugging, executing and running java programs. - Tools of JDK: javac java javadoc appletviewer jar jdb javah javap Page 3 of 10

Text from page-4

NOTES PREPARED BY ASHWINI KHADYE (B.E.- Computer) Contact : 9920139393 / 7718059393 Tools javac java javadoc appletviewer jar jdb javah javap Brief Description The compiler for the Java programming language. It is used to translate java source code to byte code files that the interpreter can understand. The launcher for Java applications. In this release, a single launcher is used both for development and deployment. The old deployment launcher, jre, is no longer provided. API documentation generator. Run and debug applets without a web browser. Manage Java Archive (JAR) files. The Java Debugger. C header and stub generator. Used to write native methods. Class file disassemble 6. What do mean by typecasting? When it is needed? (W14) (Explanation of type casting with types 2 marks, need 1 mark, 1 mark for program or code snippet) Ans: - The process of converting one data type to another is called casting or type casting. - If the two types are compatible, then java will perform the conversion automatically. It is possible to assign an int value to long variable. - However if the two types of variables are not compatible, the type conversions are not implicitly allowed, hence the need for type casting. - Eg: int m = 50; byte n = (byte) m; long count = (long) m; - Type casting is of two types: Widening, Narrowing. Widening : The process of assigning a smaller type to a larger one is known as widening Narrowing : The process of assigning a larger type into a smaller one is called narrowing. Casting is necessary when a value of one type is to be assigned to a different type of variable. It may also be needed when a method returns a type different than the one we require. Generic type casting helps in the retrieval of elements from a collection as each element in a collection is considered to be an object. Page 4 of 10

Lecture Notes