You are on page 1of 10

What is Byte-Code?

Byte-codes are the basis of Java’s platform independence. You either compile or interpret a program in most programming languages. However a Java program is both compiled and interpreted. With the compiler, a program is translated into an intermediate language called Java byte-code, which is the platform-independent code interpreted by the interpreter on the Java platform. The interpreter parses and runs each Java byte-code instruction on the computer. Compilation happens just once; interpretation occurs each time the program is executed. Essentially, Byte-codes are the machine language of the Java virtual machine. These byte-codes further get converted to the native machine code by the interpreter engine.

What is JVM
The Java virtual machine executes instructions that a Java compiler generates. Java Virtual Machine is the base for the Java platform and is ported onto various hardware-based platforms. Every Java interpreter, whether it's a development tool or a Web browser that can run applets, is an implementation of the Java VM. The Java virtual machine has n knowledge of the Java programming language. It understands a particular binary format, the class file format. A class file contains Java virtual machine instructions (or byte-codes) and a symbol table and other supportive information. JVM imposes strong security by having format and structural constraints on a class file. This leads to an interesting outcome: any language with functionality that can be expressed in terms of a valid class file can be hosted by the Java virtual machine.

What is an Object?
An object is a class variable. An object is a representation of a class that holds data and allows operations to be performed on it self. Class is a definition of a data type whereas an object is a variable or an instance of the class type. An object is a representation of a type that holds data and allows operations to be performed on it self. An Object lets us describe our concept, type or idea in terms of data and methods. Thus becomes a basis for a modular analysis of our problem. The whole problem and solution domain can be represented in terms of objects; objects can interact with themselves producing results. Example: Here Book is our class which represents a Book type with its attributes and operations. The Book becomes a user defined type variable which can be declared as an object and used to perform operations a book type.
Class Book {

String pub. Constructor is a method of a class with no return parameters.”McGraw Hill”. Constructors also enable the initialization of any variables of a class. 11.title=title. What Are Constructor Methods? Constructors from a conceptual level enable the creation of an object of a given class. A default constructor doesn’t have any parameters. It has the same name as the class itself. Book book3 = new Book(“Thinking in Java”. Book book2 = new Book(“Core Java”. This binds data and operations tightly together and separates them from external access that may corrupt them intentionally or unintentionally. In case of inheritance .20). 10. use the ‘super’ keyword. A next level of accessibility is provided by the Protected keyword which gives the derived classes the access to the member variables of the base class.if your subclass constructor does not explicitly call the superclass constructor. Encapsulation is achieved by declaring variables as Private in a class. String publisher.20). this.price=price.this gives access to data to only member functions of the class.String title.book2 and book3 are objects that represent three different Book types.”Addison Wesley”. 12. You can supply input parameters to constructors.”Sun Press”. } What is Encapsulation? Encapsulation provides the basis for again modularity. .20). public static void main(String args[]) { Book book1 = new Book(“Chicken soup for soul”. float price. If you want subclass constructor to call a specific superclass constructor.a variable declared as Protected can at most be accessed by the derived classes of the hiding information from unwanted outside access and attaching that information to only methods that need access to it. A class can have more than one constructor. java will call the super class default constructor. } setBookTitle(String title){ } In the below book1. Book(String title. float price) { this.publisher=pub this.

Example: Class Book { String title. which contain abstract methods.title = title. An abstract class lets you put the common method names in one abstract class without having to write the actual implementation code. public Book() { } public Book(String title) { this. } What is Static Modifier and Class? . } What is an Abstract Class? Java provides you with a special type of class. float price. which extends the class containing abstract methods. Non-abstract methods implement the method statements. Abstract classes may contain a mixture of non-abstract and abstract methods. are called abstract classes. must provide the implementation for all the abstract methods. String publisher. Following are the key points of abstract class – • • • • Classes. Example: public abstract Class Book{ public abstract String readBook(). public abstract void writeBook(String text). A program cannot instantiated an abstract class as an object. called an abstract class. } } Class TextBook extends Book { public TextBook(){ } public TextBook(String title) { super(title). otherwise the subclass itself becomes an abstract class. which can help you. organize your classes based on common methods. Any subclass.

method1(). When you declare a method in a class static you do not need an instance of the class. which takes care of freeing dynamically allocated memory that is no longer referenced. A class containing static members is called as a static class. The name "garbage collection" implies that objects that are no longer needed by the program are "garbage" and can be thrown away. Java programmers. should avoid writing code for which program correctness depends upon the timely finalization of objects. The garbage collector must somehow determine which objects are no longer referenced by the program and make available the heap space occupied by such un-referenced objects." When the program no longer references an object. The Java programmer must keep in mind that it is not generally possible to predict exactly when un-referenced objects will be garbage collected so it is not possible to predict when object finalizers will be run. In the process of freeing un-referenced objects. the heap space it occupies must be recycled so that the space is available for subsequent new objects. Because the heap is garbagecollected. public static method1(){ prop1 = “Hello”. } public static void main(String args[]){ MyClass. You can use static methods to access other static members of the class. Example: public Class MyClass{ public static String prop1. There is no static identifier for the class. A more accurate and up-to-date metaphor might be "memory recycling.prop1 = “Bye”. MyClass. there is a way to tell the JVM to perform garbage collection. Invoking the garbage collector requires a simple . which may result in the desired memory being reclaimed. the garbage collector must run any finalizers of objects being freed. Java programmers don't have to explicitly free allocated memory. } } What Is Garbage Collection? A key feature of Java is its garbage-collected heap. Although you can't tell Java to explicitly reclaim the memory of a specific de-referenced object.There may be times in your program where the class objects need to share information – in such cases you can specify one or more class variable and methods that are shared among the objects. however. therefore. you cannot use the static method to access any non-static variables or methods.

Java programmers are unable to accidentally (or purposely) crash the JVM by incorrectly freeing memory. The preprocessor basically performs an intelligent search and replace on identifiers that have been declared using the #define or #typedef directives. on the other hand. First you create a Java Runtime object. a file). procedural. C++ is designed to support both paradigms. Runtime r = Runtime. #define can also be used to . you write a number of procedures to do certain tasks and build up a program by calling those procedures as needed. Paradigm C is geared towards procedural programming. C++ or Java. what programming paradigm suits the application (OOP. That is. From one class many objects may be created. r. Garbage collection relieves programmers from the burden of freeing allocated memory.gc(). A second advantage of garbage collection is that it helps ensure program integrity. It is possible to write in an object oriented style in C and in a procedural style in Java. desktop software. C++ and Java. after creating the Runtime object.two-step process. C And C++ This article aims to set out some of the differences between C.getRuntime(). a string of text. The Differences Between Java. Garbage collection is an important part of Java's security strategy. Why garbage collection? First. you'll invoke the gc() method ("garbage collector") of the Runtime class. What follows should help you decide where it might be suitable to use C. etc). What it does not aim to do is conclude that one language is always the best one to use. you define classes which represent an entity (for example. is geared towards OOP (object oriented programming). Language choice depends upon a range of factors including field of application (operating systems. Java. Then. With OOP. a button. Preprocessor All C and C++ compilers implement a stage of compilation known as the preprocessor. Knowing when to explicitly free allocated memory can be very tricky. with every object of a certain class having the fields (places to store data) and methods (named blocks of code associated with the object) as defined by the class. the target platform and available programmer expertise. web applications etc). a window. but in each case the language will somewhat get in your way.

This approach prevents common errors due to pointer mismanagement. but can lead to pauses in execution while the garbage collector runs. a macro MAX(x. which can be useful. Java passes all arrays and objects by reference. This prevents memory leaks.g. Forgetting to free memory leads to memory leaks. It's powerful. Constant data members are used in place of the #define directive and class definitions are used in lieu of the #typedef directive. and in long-running programs can lead to the memory usage of the program growing very large.declare macros. Most of the preprocessor definitions in C and C++ are stored in header files. there is no promise of timely destruction in Java. Additionally.y) could be defined to return whichever of x or y holds the greatest value. Also. Pointers Most developers agree that the misuse of pointers causes the majority of bugs in C and C++ programs. however there is no substitute for macros. The Java language does not support pointers. It also makes programming easier in a lot of ways simply because the correct usage of pointers is easily misunderstood by inexperienced programmers. It considers them as aliases to another variable or object. but can lead to a lot of time spent hunting down complex and often subtle bugs that arise as a result of having unguarded memory access. it provides similar functionality by making heavy use of references. The Java approach to defining constants and naming types of data structures is probably conceptually simpler for the programmer. meaning that memory is freed automatically when it is no longer reachable by any references. which complement the actual source code files. C/C++ programmers regularly use complex pointer arithmetic to create and maintain dynamic data structures. Put simply. Bounds Checking . when you have pointers. Java does not have a preprocessor. you have the ability to attempt to access memory that isn't yours and modify memory relating to a different data structure than the one you intended by accident. using free or delete). This is not like calling a function as the substitution is done before the code is compiled. the Java compiler builds class definitions directly from the source code files. C++ does provide references too. Memory Management In C and C++. Instead. A reference can be thought of as a "safe pointer" . any memory that is allocated on the heap (e.g. They are safer than pointers where they can be used. Java programs don't use header files. For example. Java provides garbage collection.the programmer can not directly manipulate the memory address. which contain both class definitions and method implementations. using malloc or new) must be explicitly freed by the programmer (e.

Portability And Performance C and C++ both compile to native machine code. So bytecode is portable accross different operating systems and processors. Java will bounds check arrays to prevent this from happening.that is. no runtime error will occur. so attempts to access the sixth element of a 5-element array will appear to work . In Java this type is called String. but they do both have string objects that are a standard part of the language. and the memory saving advantages unions hold in C may not carry accross. The JVM has to turn instructions in the bytecode into instructions that are understood by the machine that the bytecode is running on. Strings C has no built-in string data type. The standard technique adopted among C programmers is that of using null-terminated arrays of characters to represent strings. Neither C++ or Java have string as a primitive type. which then runs on top of a virtual machine (the JVM). Java presents a simpler model but at the cost of not being able to save a little memory. now only the virtual machine (and standard library) have to be ported to different platforms. For many applications this will be a non-issue. However. especially considering the potential for buffer overflow attacks. it also restricts them to running on the platform they were compiled to run on. However. emulating a union is not always possible in Java. This gives a runtime performance penalty (although this is getting less significant as the JVM improves and computers get faster). of course with a little extra runtime cost. Multiple Inheritance . This practice if often seen in C++ programs too.simply write a class without any methods and make all the fields public. A structure can be emulated by a class .An array in C or C++ is not bounds checked. Java only implements one of these data types: classes. Java generally compiles to Java bytecode. This means that. programs written in these languages will perform very well. Complex Data Types There are two types of complex data types in C: structures and unions. This means the programmer needs to code very carefully. However. then the bytecode for many Java programs can be executed on that platform. with a good compiler. and in C++ it is called CString. C++ adds classes to this list.

However. This allows object types to feel much more tightly integrated into the language and can deliver much clearer code. it is unclear which X should be invoked. although a compiler warning will be given about this. so there is no ambiguity over which method to invoke. For example. Automatic Coercions Automatic coercion refers to the implicit casting of data types that sometimes occurs in C and C++. Java does not support C++ style automatic coercions. It is not supported in Java. Therefore implementations can only be inherited from one class. Although multiple inheritance is indeed powerful (and sometimes the logical way to define a class hierachy). in C++ you can assign a float value to an int variable. even though it may take longer to type and read. it is complicated to use correctly and can create situations where it's uncertain which method will be executed. if each of the parent classes provide a method X and the derived class does not. whereas a well-named method call would be clear. For example. which can result in a loss of information. and sometimes near impossible to predict code known as "spaghetti code. Java provides no direct support for multiple inheritance. Operator overloading is considered a prominent feature in C++." The primary bad usage of the goto statement has merely been as a convenience to substitute not thinking through an alternative. but you can implement functionality similar to multiple inheritance by using interfaces in Java. if the * (multiply) operator was to be used on two objects of type Matrix. In Java. but it is a standard part of the language. The goto statement has historically been cited as the cause for messy. Java interfaces provide method descriptions but contain no implementations. sometimes it is not clear what a particular operator would sensibly do for a particular type. difficult to understand. Very occasionally. you must always explicitly cast the data element to the new type.Multiple inheritance is a feature of some object oriented languages that allows you to derive a class from multiple parent classes. Goto Statement The goto statement is rarely used these days in C and C++. if coercion will result in a loss of data. probably in an effort to keep the language as simple as possible and help ensure it is obvious what code does. Operator Overloading Operator overloading enables a class to define special behaviour for built-in operators when they are applied to objects of that class. C++ supports multiple inheritance. then matrix multiplication could be implemented. more structured branching technique. It is also complicated to implement from the compiler perspective. it can lead to clearer code. For example. .

but its usage is not supported. unions. it is impossible for the compiler to thoroughly type check the arguments. non-messy code. unions Structures. the system passes two arguments to a program: argc and argv. though if it did it would likely be able to handle subsequent runtime problems better than C or C++. This is consistent with Java's desire to make programmers write clear. In Java. Yes. argv is a pointer to an array of characters containing the actual arguments. Generic Programming (from Java Programming 5) Executable Native Code Manual Java bytecode Managed. Java doesn't support variable arguments at all. objects Structures. the number of parameters passed differs between the two languages. references are used instead. ‘args’ is an array of Strings that contains the command-line arguments. OOP.To avoid the potential for "spaghetti code". that take a variable number of arguments. very but some form of commonly used. which means problems can arise at runtime without you knowing. Feature Paradigms C Procedural C++ Java OOP. Variadic Arguments C and C++ let you declare functions. using a garbage collector No pointers. The Java language specifies goto as a keyword. No Objects Form of Executable Compiled Source Native Code Code Memory Manual management Pointers Preprocessor String Type Complex Data Types Inheritance Yes. First. In C and C++. . Generic Procedural. Command-line Arguments The command-line arguments passed from the system into a Java program differ in a couple of ways from the command-line arguments passed into a C++ program. argc specifies the number of arguments stored in argv. Yes Yes Character arrays Character arrays. C++ and Java This table is a summary of the differences found in the article. the system passes a single value to a program: args. such as printf. classes Classes N/A Multiple class inheritance Single class inheritance. Java does not provide a goto statement. very commonly used. references available too. Table Comparing C. Although this is a convenient feature.

multiple interface implementation Operator Overloading Automatic coercions Variadic Parameters Goto Statement N/A Yes. msut cast explicitly Yes Yes No No . with warnings if loss Not at all if loss could could occur occur. with warnings if loss could occur Yes Yes Yes No Yes.