Download Unit1 - Online Guru Jee

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
Unit 1- Introduction to Java
Difference between C and C++
C++, as the name suggests is a superset of C. As a matter of fact, C++ can run most of C code
while C cannot run C++ code. Here are the 10 major differences between C++ & C.
1. C follows the procedural programming paradigm while C++ is a multi-paradigm language
(procedural as well as object oriented)
In case of C, importance is given to the steps or procedure of the program while C++ focuses on
the data rather than the process.
Also, it is easier to implement/edit the code in case of C++ for the same reason.
2. In case of C, the data is not secured while the data is secured (hidden) in C++
This difference is due to specific OOP features like Data Hiding which are not present in C.
3. C is a low-level language while C++ is a middle-level language
C is regarded as a low-level language (difficult interpretation & less user friendly) while C++ has
features of both low-level(concentration on what’s going on in the machine hardware) & highlevel languages(concentration on the program itself) & hence is regarded as a middle-level
language.
4. C uses the top-down approach while C++ uses the bottom-up approach
In case of C, the program is formulated step by step, each step is processed into detail while in
C++, the base elements are first formulated which then are linked together to give rise to larger
systems.
5. C is function-driven while C++ is object-driven
Functions are the building blocks of a C program while objects are building blocks of a C++
program.
6. C++ supports function overloading while C does not
Overloading means two functions having the same name in the same program. This can be done
only in C++ with the help of Polymorphism(an OOP feature)
7. We can use functions inside structures in C++ but not in C.
In case of C++, functions can be used inside a structure while structures cannot contain functions
in C.
8. The NAMESPACE feature in C++ is absent in case of C
C++ uses NAMESPACE which avoid name collisions. For instance, two students enrolled in the
same university cannot have the same roll number while two students in different universities
might have the same roll number. The universities are two different namespace & hence contain
the same roll number(identifier) but the same university(one namespace) cannot have two
students with the same roll number(identifier)
9. The standard input & output functions differ in the two languages
C uses scanf & printf while C++ uses cin>> & cout<< as their respective input & output
functions
10. C++ allows the use of reference variables while C does not
Reference variables allow two variable names to point to the same memory location. We cannot
use these variables in C programming.
Difference between C and Java
1. JAVA is Object-Oriented while C is procedural. Different Paradigms, that is.
Most differences between the features of the two languages arise due to the use of
different programming paradigms. C breaks down to functions while JAVA breaks down to
Objects. C is more procedure-oriented while JAVA is data-oriented.
2. Java is an Interpreted language while C is a compiled language.
We all know what a compiler does. It takes your code & translates it into something the machine
can understand-that is to say-0′s & 1′s-the machine-level code. That’s exactly what happens with
our C code-it gets ‘compiled’. While with JAVA, the code is first transformed to what is called
the bytecode. This bytecode is then executed by the JVM (Java Virtual Machine). For the same
reason, JAVA code is more portable.
3. C is a middle-level language while JAVA is a high-level language.
C is a low-level language(difficult interpretation for the user, closer significance to the machinelevel code) while JAVA is a high-level language(abstracted from the machine-level details,
closer significance to the program itself).
4. C uses the top-down approach while JAVA uses the bottom-up approach.
In C, formulating the program begins by defining the whole and then splitting them into smaller
elements. JAVA(and C++ and other OOP languages) follows the bottom-up approach where the
smaller elements combine together to form the whole.
5. Pointer go backstage in JAVA while C requires explicit handling of pointers.
When it comes to JAVA, we don’t need the *’s & &’s to deal with pointers & their addressing.
More formally, there is no pointer syntax required in JAVA. It does what it needs to do. While in
JAVA, we do create references for objects.
6. The Behind-the-scenes Memory Management with JAVA & The User-Based Memory
Management in C.
Remember ‘malloc’ & ‘free’? Those are the library calls used in C to allocate & free chunks of
memory for specific data(specified using the keyword ‘sizeof’). Hence in C, the memory is
managed by the user while JAVA uses a garbage collector that deletes the objects that no longer
have any references to them.
7. JAVA supports Method Overloading while C does not support overloading at all.
JAVA supports function or method overloading-that is we can have two or more functions with
the same name(with certain varying parameters like return types to allow the machine to
differentiate between them). That it to say, we can overload methods with the same name having
different method signatures. JAVA(unlike C++), does not support Operator Overloading while C
does not allow overloading at all.
8. Unlike C, JAVA does not support Preprocessors, & does not really them.
The preprocessor directives like #include & #define, etc are considered one of the most essential
elements of C programming. However, there are no preprocessors in JAVA. JAVA uses other
alternatives for the preprocessors. For instance, public static final is used instead of the #define
preprocessor. Java maps class names to a directory and file structure instead of the #include used
to include files in C.
9. The standard Input & Output Functions.
Although this difference might not hold any conceptual(intuitive) significance, but it’s maybe
just the tradition. C uses the printf & scanf functions as its standard input & output while JAVA
uses the System.out.print & System.in.read functions.
10. Exception Handling in JAVA and the errors & crashes in C.
When an error occurs in a Java program it results in an exception being thrown. It can then be
handled using various exception handling techniques. While in C, if there’s an error, there IS an
error.
Difference between C++ and Java
1.
2.
3.
4.
5.
Java does not support pointers while in C++ there are pointers.
In java there is no operator overloading while in C++ there is operator overloading.
In java there are no preprocessors while in C++ there are preprocessors.
Java does not support structure and union while C++ supports structure and union.
In java there are no functions (only methods within class) while in C++ there are
functions.
6. In java there is no goto statement. (In java goto and const are reserved words but unused)
while there is goto statement in C++.
7. In java there are no global variables (variable is a part of class) while in C++ there are
global variables.
8. Java includes automatic garbage collection while C++ requires explicit memory
management.
9. Java provides interfaces in case of multiple inheritance while C++ supports multiple
inheritance.
10. The main difference between C++ and Java is that "C++ does not allow persistence
because it does not support database connection while Java allows persistence because it
supports database connection."
11. Java is reusable and more reliable, more secure than C++.
What is JAVA and History of JAVA
Java is a high-level object-oriented programming language developed by the Sun
Microsystems. Earlier, C++ was widely used to write object oriented programming languages,
however, it was not a platform independent and needed to be recompiled for each different
CPUs. A team of Sun Microsystems including Patrick Naughton, Mike Sheridan in the guidance
of James Goslings decided to develop an advanced programming language for the betterment of
consumer electronic devices. They wanted to make it new software based on the power of
networks that can run on different application areas, such as computers and electronic devices. In
the year 1991 they make platform independent software and named it Oak. But later due to some
patent conflicts, it was renamed as Java and in 1995 the Java 1.0 was officially released to the
world.
Java can be used to create complete applications that may run on a single computer or be
distributed among servers and clients in a network. It can also be used to build a small
application module or applet for use as part of a Web page. Applets make it possible for a Web
page user to interact with the page.
Features of JAVA
Java is:
1. Simple
2. Object-oriented
3. Multithreaded and Interactive
4. Platform-independent and Portable
5. Dynamic
6. Secure and Robust
7. High-Performance
8. Compiled and interpreter
1. Java is simple- There are various features that makes the java as a simple language.
Programs are easy to write and debug because java does not use the pointers explicitly. Error
prone tasks such as pointers and memory management have either been eliminated or are
handled by the Java environment automatically rather than by the programmer. Java provides
the bug free system due to the strong memory management. It also has the automatic memory
allocation and deallocation system.
2. Java is Object Oriented- To be an Object Oriented language, any language must follow at
least the four characteristics.




Inheritance: It is the process of creating the new classes and using the behavior of the
existing classes by extending them just to reuse the existing code and adding the
additional features as needed.
Encapsulation: It is the mechanism of combining the information and providing the
abstraction.
Polymorphism: As the name suggest one name multiple form, Polymorphism is the way
of providing the different functionality by the functions having the same name based on
the signatures of the methods.
Dynamic binding: Sometimes we don't have the knowledge of objects about their
specific types while writing our code. It is the way of providing the maximum
functionality to a program about the specific type at runtime.
3. Multithreaded and Interactive- means handling multiple tasks simultaneously. Multithread
means that we need not wait for the application to finish one task before beginning another.
Example- we can listen to an audio clip while scrolling a page and at the same time
download an applet from computer. This feature improves the interactive performance of
graphical applications.
4. Platform-independent and Portable- Java programs can be easily transferred from one
computer to another and can be executed anywhere and anytime.
The feature Write-once-run-anywhere makes the java language portable, provided that the
system must have interpreter for the JVM.
5. Dynamic- While executing the java program the user can get the required files dynamically
from a local drive or from a computer thousands of miles away from the user just by
connecting with the Internet. Java is more dynamic than C++ or C language because it allows
dynamic linking of classes to libraries and predefined methods and objects. You can use
programs written in other programming language such as C and C++ in java, by dynamically
linking them to the java run time environment. These functions are known as native methods.
Native methods are linked dynamically at runtime.
6. Secure or Robust(Strong)- Java is a robust language. It has strict compile time and run time
checking for data types. Java also incorporates the concept of exception handling which
captures series errors and eliminates any risk of crashing the system.
Java systems not only verify all memory access but also ensure that no viruses are
communicated with an applet. The absence of pointers in java ensures that programs cannot
gain access to memory locations without proper authorization.
7. High Performance- Java uses native code usage, and lightweight process called threads. In
the beginning interpretation of bytecode resulted the performance slow but the advance
version of JVM uses the adaptive and just in time compilation technique that improves the
performance.
8. Compiled and Interpreted- We all know that Java is an interpreted language as well
compiled language.
Java compiler translates the program source code into a compiled format known as bytecode
instructions. The bytecode instructions can be executed by using Java virtual machine (JVM),
on any system.
As java is an interpreted language, interpreter converts the bytecode instructions into the
machine code that can be understood by the machine and execute the programs.
Another advantage of Java as an interpreted language is its error debugging quality. Due to
this any error occurring in the program gets traced. This is how it is different to work with
Java.
What is JDK (Java Development Kit)
JDK is a software development program provided by Sun Microsystems. Java Development Kit
or JDK comes in various version and can be downloaded free from the Sun Microsystems. JVM
compiler, debugger and other tools are used with JDK for developing java based application &
java applets. So make sure that your JVM compiler & JDK versions are same.
Java program and these tools are used by Java Runtime Environment (JRE). JRE is a software
that is used to execute the java programs.
Acronyms:
JDK Java Development Kit
JVM Java virtual machine
JRE Java Runtime Environment
JDK contents
The JDK has as its primary components a collection of programming tools, including:

javac – the compiler, which converts source code into Java bytecode

java – the loader for Java applications. This tool is an interpreter and can interpret the class
files generated by the javac compiler.

appletviewer – this tool can be used to run and debug Java applets without a web browser

javadoc – the documentation generator, which automatically generates documentation
from source code comments

javah – the C header and stub generator, used to write native methods

javap – the class file disassembler.
jdb – the debugger that finds error in program.

JVM (Java Virtual Machine) - An abstract computing machine, or virtual machine, JVM
is a platform-independent execution environment that converts Java bytecode into machine
language and executes it. Most programming languages compile source code directly into
machine code that is designed to run on a specific microprocessor architecture or operating
system, such as Windows or UNIX. A JVM -- a machine within a machine -- mimics a real Java
processor, enabling Java bytecode to be executed as actions or operating system calls on any
processor regardless of the operating system. For example, establishing a socket connection from
a workstation to a remote machine involves an operating system call. Since different operating
systems handle sockets in different ways, the JVM translates the programming code so that the
two machines that may be on different platforms are able to connect.
Compilation Using Java and execution using Java
In Java, programs are not compiled into executable files they are compiled into Bytecode which
the JVM then executes at runtime. Java source code is compiled into bytecode when we use
the javac compiler. The bytecode gets saved on the disk with the file extension .class. When the
program is to be run, the bytecode is converted, using the Just-In-Time(JIT) compiler. The result
is machine code which is then fed to the memory and is executed.
So Java has two step compilations:

Step one to create byte-code

Step two to create machine level code
The Java classes/Byte Codes are compiled to machine code and loaded into memory by the JVM
when needed the first time. This is different than other languages like C/C++ where the whole
program had to be compiled to machine code and linked to create an executable file, before the
program could start.
JIT compilers compile byte-code once and the compiled machine code are re-used again and
again, to speed up execution. Early Java compilers compiled the byte-code to machine code each
time it was used, but more modern compilers cache this machine code for reuse on the machine.
Even then, java's JIT compiling was still faster than an "interpreter-language", where code is
compiled from high level language, instead of from byte-code each time it was used.
Bytecode also known as p-code (portable code), is a term which has been used to denote various
forms of instruction sets designed for efficient execution by a software interpreter as well as
being suitable for further compilation into machine code.
Execution
A bytecode program may be executed by parsing and directly executing the instructions, one at a
time. This kind of bytecode interpreter is very portable. Some systems, called dynamic
translators, or "just-in-time" (JIT) compilers, translate bytecode into machine language as
necessary at runtime: this makes the virtual machine unportable, but doesn't lose the portability
of the bytecode itself. For example, Java and Smalltalk code is typically stored in bytecoded
format, which is typically then JIT compiled to translate the bytecode to machine code before
execution.