Kayıtlar

java etiketine sahip yayınlar gösteriliyor

void... params in java

There are several ways to simulate optional parameters in Java: Method overloading. void foo ( String a , Integer b ) { //... } void foo ( String a ) { foo ( a , 0 ); // here, 0 is a default value for b } foo ( "a" , 2 ); foo ( "a" ); One of the limitations of this approach is that it doesn't work if you have two optional parameters of the same type and any of them can be omitted. Varargs. a) All optional parameters are of the same type: void foo ( String a , Integer ... b ) { Integer b1 = b . length > 0 ? b [ 0 ] : 0 ; Integer b2 = b . length > 1 ? b [ 1 ] : 0 ; //... } foo ( "a" ); foo ( "a" , 1 , 2 ); b) Types of optional parameters may be different: void foo ( String a , Object ... b ) { Integer b1 = 0 ; String b2 = "" ; if ( b . length > 0 ) { if (!( b [ 0 ] instanceof Integer )) { ...

Setting JAVA_HOME Variable in Windows

In this example we will set the JAVA_HOME variable in Windows. The JAVA_HOME variable is used by applications to find the Java Development Kit installation. Find JDK Installation Directory First you need to know the installation path for the Java Development Kit. Open the default installation path for the Java Development Kit C:\Program Files\Java or C:\Program Files (x86)\Java There should be a subdirectory like C:\Program Files\Java\jre6 or C:\Program Files (x86)\Java\jre6 Set the JAVA_HOME Variable Once you have the JDK installation path: Right-click the My Computer icon on your desktop and select Properties. Click the Advanced tab. Click the Environment Variables button. Under System Variables , click New. Enter the variable name as JAVA_HOME . Enter the variable value (one of the paths mentioned above) as the installation path for the Java Development Kit. Click OK . Click Apply Changes . You might ...

Parsing a URL

Sample - 1 import java.net.*; import java.io.*; public class ParseURL { public static void main(String[] args) throws Exception { URL aURL = new URL("http://example.com:80/docs/books/tutorial" + "/index.html?name=networking#DOWNLOADING"); System.out.println("protocol = " + aURL.getProtocol()); System.out.println("authority = " + aURL.getAuthority()); System.out.println("host = " + aURL.getHost()); System.out.println("port = " + aURL.getPort()); System.out.println("path = " + aURL.getPath()); System.out.println("query = " + aURL.getQuery()); System.out.println("filename = " + aURL.getFile()); System.out.println("ref = " + aURL.getRef()); } } Here is the output displayed by the program: protocol = http authority = example.com:80 host = example.com port = 80 path = /docs/boo...

Quick and easy image resizing with Java ImageIO

Quick and easy image resizing with Java ImageIO There are quite a lot of examples of image resizing floating around the web but they all seem to access all sort of hidden features and classes deep inside the JDK to achieve their goal. Here is the most straightforward method that I could come up with - all in all just 5 lines of code. It requires ImageIO and requires Java 1.4 or later to run. If the ImageIO.write() method accepted plain ol' AWT Image arguments it could have been reduced further still to three lines, but you have to give it a BufferedImage instance as input. The snippet below assumes that there exist the following variables (coloured green in the code): an input stream to read from a desired width variable (specifying the height as -1 tells the toolkit to preserve the original aspect ratio) and an output stream to write the encoded image to. Sample code: import java.awt.Image; import java.awt.image.BufferedImage; import javax.imageio.ImageIO; // .....

How to loop ArrayList in Java

No nonsense, four ways to loop ArrayList in Java For loop For loop (Advance) While loop Iterator loop package com.mkyong.core ;   import java.util.ArrayList ; import java.util.Iterator ; import java.util.List ;   public class ArrayListLoopingExample { public static void main ( String [ ] args ) {   List < String > list = new ArrayList < String > ( ) ; list. add ( "Text 1" ) ; list. add ( "Text 2" ) ; list. add ( "Text 3" ) ;   System . out . println ( "#1 normal for loop" ) ; for ( int i = 0 ; i < list. size ( ) ; i ++ ) { System . out . println ( list. get ( i ) ) ; }   System . out . println ( "#2 advance for loop" ) ; for ( String temp : list ) { System . out . println ( temp ) ; }   System . out . println ( "#3 while loop" ) ; int j = 0 ; while ( list. size ( ) > j ) { System . out . println ( list. get ( j ...

set JAVA for Suse

 sudo update-alternatives --install "/usr/bin/java" "java" "/opt/jdk1.7.0_05/bin/java" 3  sudo update-alternatives --config java