Docsity

Comparison of C++ and Java, Study notes of Computer science

Toggle sidebar
bg1
What is difference between c++ and Java ?
Java C++
1) Java is platform independent
1) C++ is platform dependent.
2) There are no pointers in java
2) There are pointers in C++.
3) There is no operator overloading in java
3) C ++ has operator overloading.
4) There is garbage collection in java
4) There is no garbage collection
5) Supports multithreading
5) Does’nt support multithreading
6) There are no templates in java
6) There are templates in java
7) There are no global variables in java
7) There are global variables in c
What is JIT compiler ?
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15

Partial preview of the text

Download Comparison of C++ and Java and more Study notes Computer science in PDF only on Docsity!

What is difference between c++ and Java? Java C++

  1. Java is platform independent
  2. C++ is platform dependent.
  3. There are no pointers in java
  4. There are pointers in C++.
  5. There is no operator overloading in java
  6. C ++ has operator overloading.
  7. There is garbage collection in java
  8. There is no garbage collection
  9. Supports multithreading
  10. Does’nt support multithreading
  11. There are no templates in java
  12. There are templates in java
  13. There are no global variables in java
  14. There are global variables in c What is JIT compiler?

JIT compiler stands for Just in time compiler. JIT compiler compiles byte code in to executable code. JIT a part of JVM .JIT cannot convert complete java program in to executable code it converts as and when it is needed during execution. What is bytecode in java? When a javac compiler compiles a class it generates .class file, This .class file contains set of instructions called byte code. Byte code is a machine independent language and contains set of instructions which are to be executed only by JVM. JVM can understand this byte codes. Introduction Java is an class-based object-oriented, , general-purpose programming language. Java was originally developed by James Gosling at Sun Microsystems ,Java programming language is based on the write once, run anywhere (WORA) principle, meaning that compiled Java code can run on all platforms that support Java without the need for recompilation. Java applications are typically compiled to bytecode that can run on any Java Virtual Machine (JVM) regardless of the underlying operating system.

Features of Java Simple Java is very easy to learn, and its syntax is simple, clean and easy to understand. According to Sun Microsystem, Java language is a simple programming language because: There is no need to remove unreferenced objects because there is an Automatic Garbage Collection in Java. Object-Oriented: Java is an object-oriented programming language. Everything in java is an object. Object-oriented programming language simplifies software development and maintenance by providing some rules. Platform independent: Java is a platform-independent programming language. We can write java code once and run it anywhere. Java code can be run on multiple platforms, for example, Windows, Linux, Mac, etc. Java code is compiled by the compiler and converted into byte code. This byte code is platform-independent code because it can be run on multiple platforms. Robust: Java programming language is robust because it uses strong memory management. There is an automatic garbage collection

in java to destroy the objects which are unused. Exception handling is another feature that makes java programming robust. Portable: Java programming language is portable because it enables you to carry byte code to any platform. Multithreaded: Java also supports multithreaded programming. We can create multiple threads in java. The main advantage of multithreading is that it doesn’t occupy the memory for each thread. It shares a common memory area. Threads are important for web applications, multimedia applications, etc. Distributed: Java programming language is distributed because it facilitates the user to create distributed applications. We can create distributed applications in java by using RMI and EJB. Secure: When a java program is compiled, it generates a byte code that is in the non-readable form. The java byte code cannot be read by humans. That makes the java language secure. JDK:

visibility of the method. The JVM calls the main() method outside the class. Therefore it is necessary to make the java main() method as public. static: static is a keyword in java. We can make static variables and static methods in java with the help of the static keyword. The main advantage of a static method is that we can call this method without creating an object of the class. JVM calls the main() method without creating an object of the class, therefore it is necessary to make the java main() method static. void: void is a return type of method. The java main() method doesn’t return any value. Therefore, it is necessary to have a void return type. main: main is the name of the method. It is a method where program execution starts. String args[]: String in java is a class that is used to work on Strings and args is a reference variable that refers to an array of type String. If you want to pass the argument through the command line then it is necessary to make the argument of the main() method as

String args[]. Java Variables Variable is a name of memory location. There are three types of variables in java: local, instance and static.

  1. Local Variable A variable declared inside the body of the method is called local variable. You can use this variable only within that method A local variable cannot be defined with "static" keyword.
  2. Instance Variable A variable declared inside the class but outside the body of the method, is called an instance variable. It is not declared as static.
  3. Static variable A variable that is declared as static is called a static variable. It cannot be local. You can create a single copy of the static variable and share it among all the instances of the class. Memory allocation for static variables happens only once when the class is loaded in the memory. Data Types in Java Data types specify the different sizes and values that can be stored in the variable.

predefined words by Java so they cannot be used as a variable or object name or class name. Decision Making statements decision-making statements decide which statement to execute and when. Decision-making statements evaluate the Boolean expression · if statement In Java, the "if" statement is used to evaluate a condition,The condition of the If statement gives a boolean value, either true or false. · switch statement · The switch statement contains multiple blocks of code called cases and a single case is executed based on the variable which is being switched. Loop statements loop statements are used to execute the set of instructions in a repeated order. · do while loop · while loop · for loop · for-each loop

Jump statements Jump statements are used to transfer the control of the program to the specific statements. · break statement · continue statement TypeCasting in Java TypeCasting in Java is the process of converting one primitive data type into another. TypeCasting can be done automatically and explicitly. There are two types of TypeCasting in Java. Widening or Automatic Type Conversion. Narrowing or Explicit Type Conversion.

  1. Widening or Automatic Type Conversion: When we assign a value of a smaller data type to a large data type, this process is known as Widening Type Casting. It is also known as Automatic Type Conversion because the Java compiler will perform the conversion automatically. This can happen only when the two data types are compatible.

Output Int value : 2147483647 Long value : 2147483648 Double value : 2.147483648E

  1. Narrowing or Explicit Type Conversion: When we assign a value of a large data type to a small data type, the process is known as Narrowing Type Casting. This can’t be done automatically. We need to convert the type explicitly. If we don’t perform casting, the java compiler will give a compile-time error. double -> float -> long -> int -> short -> byte ( Narrowing or Explicit Type Conversion) Example: public class ExplicitConversation { public static void main(String args[]) { // Explicit Type Conversion double d = 25.123; int i = (int) d; byte b = (byte) i;

System.out.println("Double value : " + d); System.out.println("Int value : " + i); System.out.println("Byte value : " + b); } } Output Double value : 25. Int value : 25 Byte value : 25 Overflow and Underflow in Java Overflow in java happens when we assign a value to a variable that is more than its range and Underflow is opposite to overflow. In case of overflow and underflow, the Java compiler doesn’t throw any error. It simply changes the value. For example, in the case of an int variable, its size is 4 bytes or 32 bits. The maximum value of int data type is 2,147,483, (Integer.MAX_VALUE) and after incrementing 1 on this value, it will return -2,147,483,648 (Integer.MIN_VALUE). This is known as overflow. The minimum value of int data type is - 2,147,483,648 (Integer.MIN_VALUE) and after decrementing 1 on this value, it will return 2,147,483,647 (Integer.MAX_VALUE).

· Arithmetic Operators · Unary Operators · Assignment Operators · Relational Operators · Logical Operators · Bitwise Operators · Ternary Operators · Instance of Operators Arithmetic Operators Arithmetic Operators in Java are used to perform mathematical operations, · Addition · Subtraction · Multiplication · Division · Modulus Operator (%) : Unary Operators Unary operators are work only one bit +,-,++,--

Bitwise operators Bitwise op. are used to perform operation on individual bits of a no. · & AND · | OR · ^ XOR · ~ NOT · << left shift · >>RIGHT SHIFT Relational operators Always give boolean type value <,>,<=,>= Logical op. ! , &&, || Ternary Operator Java Ternary operator is also known as the conditional operator: This operator is the only conditional operator in java that takes three operands. We can use ternary operator in place of if-else statement or even switch statement. The syntax of the ternary operator is shown below:

An Object can be defined as an instance of a class. An object contains an address and takes up some space in memory. Any entity that has state and behavior is known as an object. For example, a chair, pen, table, keyboard, bike, etc. It can be physical or logical. Class Collection of objects is called class. It is a logical entity. A class can also be defined as a blueprint from which you can create an individual object. Class doesn't consume any space. Inheritance When one object acquires all the properties and behaviors of a parent object, it is known as inheritance. It provides code reusability. It is used to achieve runtime polymorphism. Polymorphism If one task is performed in different ways, it is known as polymorphism. In Java, we use method overloading and method overriding to achieve polymorphism. new keyword in Java The new keyword is used to allocate memory at runtime. All objects get memory in Heap memory area.

Anonymous object Anonymous simply means nameless. An object which has no reference is known as an anonymous object. It can be used at the time of object creation only. example class Calculation{ void fact(int n){ int fact=1; for(int i=1;i<=n;i++){ fact=fact*i; } System.out.println("factorial is "+fact); } public static void main(String args[]){ new Calculation().fact(5);//calling method with anonymous object } } Garbage colletcter =