import java.io.Console;
import java.lang.String;
import java.io.IOException;
public class input1 {
public static void main (String args[]) throws IOException {
Console c = System.console();
if (c == null) {
System.err.println("No console.");
System.exit(1);
}
String login = c.readLine("Enter your login: ");
String n=console.readline("enter age");
System.out.print("name is",login);
c.format("name is %s",login);
c.format("name is %s",n);
}
}
Monday, June 15, 2009
Example for instanceof
class InstanceofDemo {
public static void main(String[] args) {
Parent obj1 = new Parent();
Parent obj2 = new Child();
System.out.println("obj1 instanceof Parent: " + (obj1 instanceof Parent));
System.out.println("obj1 instanceof Child: " + (obj1 instanceof Child));
System.out.println("obj1 instanceof MyInterface: " + (obj1 instanceof MyInterface));
System.out.println("obj2 instanceof Parent: " + (obj2 instanceof Parent));
System.out.println("obj2 instanceof Child: " + (obj2 instanceof Child));
System.out.println("obj2 instanceof MyInterface: " + (obj2 instanceof MyInterface));
}
}
class Parent{}
class Child extends Parent implements MyInterface{}
interface MyInterface{}
public static void main(String[] args) {
Parent obj1 = new Parent();
Parent obj2 = new Child();
System.out.println("obj1 instanceof Parent: " + (obj1 instanceof Parent));
System.out.println("obj1 instanceof Child: " + (obj1 instanceof Child));
System.out.println("obj1 instanceof MyInterface: " + (obj1 instanceof MyInterface));
System.out.println("obj2 instanceof Parent: " + (obj2 instanceof Parent));
System.out.println("obj2 instanceof Child: " + (obj2 instanceof Child));
System.out.println("obj2 instanceof MyInterface: " + (obj2 instanceof MyInterface));
}
}
class Parent{}
class Child extends Parent implements MyInterface{}
interface MyInterface{}
ReverseString
public class ReverseStringTest {
public static void main(String[] args) {
String str = "What's going on?";
System.out.println(ReverseString.reverseIt(str));
}
}
class ReverseString {
public static String reverseIt(String source) {
int i, len = source.length();
StringBuffer dest = new StringBuffer(len);
for (i = (len - 1); i >= 0; i--)
dest.append(source.charAt(i));
return dest.toString();
}
public static void main(String[] args) {
String str = "What's going on?";
System.out.println(ReverseString.reverseIt(str));
}
}
class ReverseString {
public static String reverseIt(String source) {
int i, len = source.length();
StringBuffer dest = new StringBuffer(len);
for (i = (len - 1); i >= 0; i--)
dest.append(source.charAt(i));
return dest.toString();
}
Example of Garbage Collection
import java.io.*;
import java.lang.*;
class GC
{
public static void main(String s[])
{
Runtime r=Runtime.getRuntime();
System.out.println("Total Memory:" +r.totalMemory());
System.out.println("Free Momery before allocation:" +r.freeMemory());
String str[] = new String[20];
for(int i=0;i<20;i++)
{
str[i]=new String("String Object No. "+i);
System.out.println(str[i]);
}
System.out.println("Free Momery after allocation:" +r.freeMemory());
for(int i=0;i<20;i++)
str[i]=null;
r.gc();
System.out.println("Free Momery after Garbage Collection:" +r.freeMemory());
}
}
import java.lang.*;
class GC
{
public static void main(String s[])
{
Runtime r=Runtime.getRuntime();
System.out.println("Total Memory:" +r.totalMemory());
System.out.println("Free Momery before allocation:" +r.freeMemory());
String str[] = new String[20];
for(int i=0;i<20;i++)
{
str[i]=new String("String Object No. "+i);
System.out.println(str[i]);
}
System.out.println("Free Momery after allocation:" +r.freeMemory());
for(int i=0;i<20;i++)
str[i]=null;
r.gc();
System.out.println("Free Momery after Garbage Collection:" +r.freeMemory());
}
}
Simple Frame
import java.awt.*;
public class FrameExample{
Frame f;
public FrameExample(){
f=new Frame("Hello ");
}
public void launchFrame(){
f.setSize(400,170);
f.setBackground(Color.blue);
f.setVisible(true);
}
public static void main(String args[]){
FrameExample guiwindow=new FrameExample();
guiwindow.launchFrame();
public class FrameExample{
Frame f;
public FrameExample(){
f=new Frame("Hello ");
}
public void launchFrame(){
f.setSize(400,170);
f.setBackground(Color.blue);
f.setVisible(true);
}
public static void main(String args[]){
FrameExample guiwindow=new FrameExample();
guiwindow.launchFrame();
Subscribe to:
Posts (Atom)