TechieDrill Your World Of Technical Tutorials

Posts Tagged ‘Java’

Java Console Tricks

11.22.2009 · Posted in Java

import java.io.*; class ConsoleDemo { public static void main(String args[]) { String str; Console con; con = System.console(); if(con ==null) return; str = con.readLine(”Enter a string”); con.printf(”Here is your string: %s\n”, str); System.out.println(”Please enter password…”); char cpass[] = con.readPassword(”****************************”); System.out.println(”Your password is : “); for ( int i=0; i<cpass.length; i++) System.out.println(cpass[i]); } } This is a cool java trick. ...

ByteArrayInputStream

11.22.2009 · Posted in Java

import java.io.*; class ByteArrayInputStreamDemo { public static void main(String [] args) throws IOException { byte data[]=”Here is a String”.getBytes(); ByteArrayInputStream in = new ByteArrayInputStream(data); int ch; while((ch = in.read()) !=-1) { System.out.println(ch+ ” “); System.out.println((char)ch+” “); } } }import java.io.*; class ByteArrayInputStreamDemo { public static void main(String [] args) throws IOException { byte data[]=”Here is a String”.getBytes(); ByteArrayInputStream in = ...

Java Assertions

11.22.2009 · Posted in Java

Assertions are typically a predictable statements that we can program. Assertions facilitate to test our assumptions about our program. Assertions holds a boolean value which we assume it to be true, if it turn it to be false then interpreter will throw an exception. Having assertions in programs will be feasible for us to detect and correct ...

Understanding Java Static block/variables/method

11.20.2009 · Posted in Java

lass UseStatic { static int a=3; static int b; static void meth(int x) { System.out.println(”x: “+x); System.out.println(”a: “+a); System.out.println(”b: “+b); } static { System.out.println(”static block initialized: “); b=a*4; } public static void main(String [] args) { meth(42); } } Static block/variables/methods are very powerful in java. When we say static it is static throughout the application. If you declare ...

Java Variable Arguments Example

11.20.2009 · Posted in Java

lass VarArgs { static void vaTest(String msg, int… v) { System.out.println(msg +v.length+ ” Contents:”); for(int x : v) System.out.print(x+ ” “); System.out.println(); } public static void main(String args[]) { vaTest(”One vaarg:”, 10); vaTest(”Three varrgs:”,1,2,3); vaTest(”No varrgs”); } } Variable arguments or in short VarArgs introduced in J2SE1.5. This feature is very helpful for developers to declare as much as parameters ...

Launching Applications from Java Runtimes

11.20.2009 · Posted in Java

You can run any Applications by executing Java Runtime Classes See the code below class RuntimeDemo { public static void main(String[] args) throws Exception { Runtime r =Runtime.getRuntime(); Process p=null; p=r.exec(”notepad.exe”); } } Copy and paste the above piece of de and execute the java interpreter class RuntimeDemo { public static void main(String[] args) throws Exception { Runtime r =Runtime.getRuntime(); Process p=null; ...