Java Programming

  • Uploaded by: Vemula RenaSagar
  • 0
  • 0
  • May 2020
  • PDF

This document was uploaded by user and they confirmed that they have the permission to share it. If you are author or own the copyright of this book, please report to us by using this DMCA report form. Report DMCA


Overview

Download & View Java Programming as PDF for free.

More details

  • Words: 917
  • Pages: 3
Java is an object-oriented programming language developed by Sun Microsystems. Java is Platform independent, Platform independence means, the ability of a program to move easily from one computer system to another-is one of the most significant advantages that Java has over other programming languages, particularly if your software needs to run on many different platforms. Java can be used to create two types of programs: applets and stand-alone applications. An Applet is simply a part of a Web page, just as an image or a line of text can be. Just as a browser takes care of displaying an image referenced in an HTML document, a Java-enabled browser locates and runs an Applet . When your Java-capable Web browser loads the HTML document, the Java applet is also loaded and executed. Applets are Java programs that execute within the context of a Web page. Java arrays An array is simply a sequence of either objects or primitives, all the same type and packaged together under one identifier name. Arrays are defined and used with the square-brackets indexing operator [ ]. To define an array you simply follow your type name with empty square brackets: int[] a1; You can also put the square brackets after the identifier to produce exactly the same meaning: int a1[]; int[] a1 = { 1, 2, 3, 4, 5 }; Here are some of the major differences between Java, C, and C++: • Java has no pointers. • Java has no global variables and no global functions. • Java supports method overloading. • Java has no preprocessor. Thus, there are no macros (#define). so it doesn’t need #include. • In Java, instance (nonstatic) methods are bound dynamically by default. • In Java, a String is an object, and not an array of char. java.applet This package contains the classes necessary for creating applets. java.awt The extension awt stands for Abstract Windowing Toolkit. java.awt.image This package provides classes for performing image processing. java.io The java.io package is the Java version of stdio.h (if you use C) or stream.h (C++). In other words, here are the classes for performing input and output. java.lang This package is automatically imported by the Java compiler. java.net The java.net package contains classes for interfacing with the Internet and the World Wide Web. java.util In this package you’ll find declarations of basic data structures that come in really handy, such as a stack and a hashtable. The Java Interpreter Once you’ve written a Java program, you’ll want to run it! There are few steps

involved. First, compile the source file with the Java compiler. The compiler produces bytecode, which is stored in a class file. Then, invoke the Java interpreter for this class file. You can explicitly invoke the Java interpreter from a command-line prompt, or let a Web browser do it for you. Then, the Java interpreter takes over. It does a few things: 1. The appropriate class file is loaded by the bytecode loader. The class file can come from the local file system or from the Internet. 2. Since the bytecode may be of unknown origin, it is checked by the bytecode verifier. Code that passes the tests of the verifier can be safely executed. In effect, the bytecode verifier acts as a gatekeeper, which prevents nasty, damaging programs from being executed. 3. The successfully verified bytecode is executed by the implementation of the Java virtual machine. These components—loader, verifier, and virtual machine—work in conjunction so that classes are dynamically loaded and linked as needed. Program 1: Your First Java Program class Fun { public static void main(String argv[]) { System.out.println("Java is FUN!"); } } Store this short program in a file called Fun.java. The name of the file must match the name of the class. Now, compile it by typing % javac Fun.java The Java compiler, javac, produces a file that’s called Fun.class, which contains the bytecode. To run your program, type % java Fun Now java, the Java interpreter, locates the file Fun.class, and starts execution from the main() method. The result is Java is FUN! The class Fun defines a method called main(). What meaning do the three keywords in front of main() have? • public. This is an access specifier that states that any object can invoke main(). • static. This means that main() is a class method, and can be invoked even if an instance of the class isn’t allocated. • void. This means that main() doesn’t return anything. Inside main() is the line System.out.println("Java is FUN!"); Q: What is the default value of the local variables? A: The local variables are not initialized to any default value, neither primitives nor object references. If you try to use these variables without initializing them explicitly, the java compiler will not compile the code. It will complain abt the local varaible not being initilized..

Q: How many objects are created in the following piece of code? MyClass c1, c2, c3; c1 = new MyClass (); c3 = new MyClass (); A: Only 2 objects are created, c1 and c3. The reference c2 is only declared and not initialized. Q: What will be the output of the following statement? System.out.println ("1" + 3); A: It will print 13. Q: What happens if you dont initialize an instance variable of any of the primitive types in Java? A: Java by default initializes it to the default value for that primitive type. Thus an int will be initialized to 0, a boolean will be initialized to false.

Related Documents


More Documents from ""