Home Java Example Java Util Getting and Setting Java Type Values in a Preference
Questions:Ask|Latest


 
 

Share on Google+Share on Google+

Getting and Setting Java Type Values in a Preference

Advertisement
As you know that a preference node holds only string values. Therefore the Preferences class provides some convenient methods to convert the Java types into string.

Getting and Setting Java Type Values in a Preference

     

This section demonstrates you to set and get the Java Type values in a Preference. 

As you know that a preference node holds only string values. Therefore the Preferences class provides some convenient methods to convert the Java types into string. For example, the method prefs.putBoolean("Boolean", true)  converts a Boolean value into a string and then saves the string value and the method prefs.putBoolean("Boolean", true) converts the string back into the Boolean value. Similarly the other Java types are converted into the string. You can see in the given example that we have used the conversion methods for Boolean, int, long, float, double. To specify all these Java  Types in the Preference tree, we have define a node 'Java Types'.

 

 

 

 

Here is the code of JavaTypes.java

import java.util.prefs.*;
import java.io.*;
public class JavaTypes{
public static void main(String []args){

 Preferences prefs = Preferences.userRoot().node("/Java Types");
 prefs.put("String""Hello");  
 prefs.putBoolean("Boolean"true); 
 prefs.putInt("Integer"1);  
 prefs.putLong("Long"123456L); 
 prefs.putFloat("Float"12.3F);  
 prefs.putDouble("Double"12.3); 
 
 String s = prefs.get("String""Hello"); 
 boolean b = prefs.getBoolean("Boolean"true); 
 int i = prefs.getInt("Integer"1);  
 long l = prefs.getLong("Long"123456L); 
 float f = prefs.getFloat("Float"12.5F);  
 double d = prefs.getDouble("Double"12.5);  

System.out.println(s+" = is of String type");
System.out.println(b+" = is of Boolean type");
System.out.println(i+" = is of Integer type");
System.out.println(l+" = is of Long type");
System.out.println(f+" = is of Float type");
System.out.println(d+" = is of Double type");
}
}

Output will be displayed as:

Download Source Code:

 

Advertisements

Liked it!  Share this Tutorial


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: January 7, 2009

Ask Questions?    Discuss: Getting and Setting Java Type Values in a Preference  

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments
DMCA.com