Posts

Scanner Class and Static Import with Example

java.util.Scanner class provides convenient methods to read data in the form of primitive type as well as string from the keyboard. A simple text scanner which can parse primitive types and strings using regular expressions. A Scanner breaks its input into tokens using a delimiter pattern, which by default matches whitespace. The resulting tokens may then be converted into values of different types using the various next methods. Constructor: public Scanner(InputStream) Methods:

What is Applet with Simple Example ?

An Applet is a java program that is transmitted over the network from server to the client and execute within browser of client. Applets are used to provide interactivity in web application. It is a managed object. Managed object is an object that is created and managed by a run time environment.   Lifecycle methods are used to manage the lifecycle of a managed object. Applets are managed object because their objects are created and managed by the Browser or another Java applet viewing program, such as the Applet Viewer. Lifecycle Methods of Applet :

Command Line Arguments in NetBeans IDE

Image
Problem of a student: I need to pass the command line arguments to my java program,   I'm using netbeans IDE, anyone please let me know how to pass comand line arguments to our program(to main method) in netbeans.   Solution: We will create a new Java Application project and will add some code to the Main method which will print the provided command line arguments on the console. Steps:

What is J2ME with simple example ?

The Java 2 Micro Edition (J2ME): J2ME is a Java platform that is designed for small devices. It contains specially designed, lightweight virtual machines; a bare minimum of core-class libraries; and lightweight substitutes for standard Java libraries. J2ME is the ideal mobile client platform for wireless PDAs and enhanced mobile phones. J2ME is divided into configurations , profiles , and optional APIs, which provide specific information about APIs and different families of devices.   A configuration is designed for a specific kind of device based on memory constraints and processor power. It specifies a Java Virtual Machine (JVM) that can be easily ported to devices supporting the configuration. It also specifies some subset of the Java 2 Platform, Standard Edition (J2SE) APIs that will be used on the platform, as well as additional APIs that may be necessary. Profiles are more specific than configurations. A profile is based on a configuration and adds APIs for user inte...

Can we override static method in Java ?

In sort,  Static method belongs to the class, rather than to an object of the class. As you know overriding a method means redefining behaviour of an object, but static method show the behaviour of a class as whole and it is referred by class name. So it does not redefine the behaviour of an object. In details, Static method should be invoked with the class name, without the need for creating an instance of the class, as below ClassName.methodName(if args) You can also refer to static methods with an object reference, as bellow instanceName.methodName(if args) A subclass inherits all the members including static member (fields, methods, and nested classes) from its superclass.

How to define custom Exception ?

To define custom exception crate a sub class of exception and either override toString() method or provide default as well as augmented constructor. Argumented constructor must receive message as String. public class MyException extends Exception{     String msg="Exception";     public MyException() {         super();     }     public MyException(String msg){         super(msg);     } //or     @Override     public String toString(){         return msg;     } }

Magic of Finally block

‘finally’ keyword is use to define a block of statement that is to be executed when an exception occurs or non. Magic  of finally block public class Main {     public static int divide(int x, int y) throws Exception{         boolean flag=false;         try{             if(y==0)                 throw new ArithmeticException("divide by zero");             int c = x / y;             return c;         }catch(Exception e){             System.out.println("Excetion is rethrowing..");             flag=true;             throw(e);         }finally{             if(flag==true){...