QueryCAT Logo
Search 5,000,000+ questions and answers.

Frequently Asked Questions

Sect. 18) How do I print the hex value of an int?

Java Programmer's FAQ - Part D
You can print the hex equivalent of an int with: int i = 0xf1; System.out.println("i is hex " + Integer.toHexString(i) );

Sect. 18) How do I convert a String to an int?

Java Programmer's FAQ - Part D
There are several ways. The most straightforward is: String myString = numString.trim(); int i = Integer.parseInt(myString); long l = Long.parseLong(myString) or String myString = numString.trim(); i = Integer.parseInt(myString,myIntRadix); Note 1: There is a gotcha with parseInt - it will throw a NumberFormatException for String values in the range "80000000" to "ffffffff". You might expect it to interpret them as negative, but it does not. The values have to be "-80000000" . See similar questions...

Sect. 18) How do I convert an int to a string?

Java Programmer's FAQ - Part D
Try any of these: String s = String.valueOf(i); or String s = Integer.toString(i); or String s = Integer.toString(i, radix); or // briefer but may result in extra object allocation. String s = "" + i; Note: There are similar classes for Double, Float, Long, etc. See similar questions...

Sect. 18) What are the naming conventions?

Java Programmer's FAQ - Part D
Package names are guaranteed uniqueness by using the Internet domain name in reverse order: com.javasoft.jag - the "com" or "edu" (etc.) part used to be in upper case, but now lower case is the recommendation. Class and interface names are descriptive nouns, with the first letter of each word capitalized: PolarCoords. Interfaces are often called "something-able", e.g. "Observable", "Runnable", "Sortable". See similar questions...

Sect. 18) How can I set a system property?

Java Programmer's FAQ - Part D
JDK 1.2 has System.setProperty( "property", "new value" ); Until then, you can get all the properties, and set just the one you want with code like this: System.getProperties().put("property", "new value" ); See similar questions...

Sect. 18) How do I execute a command in my program?

Java Programmer's FAQ - Part D
Use Runtime.getRuntime().exec( myCommandString ) where myCommandString is something like "/full/pathname/command". An applet will need to be signed in order to allow this. If the pathname contains spaces, e.g. "c:\program files\windows\notepad", then enclose it in quotes within the quoted string. Or pre-tokenize them into elements of an array and call exec(String[] cmd) instead of exec(String cmd). From JDK1.3 there are two new overloaded Runtime.exec() methods. See similar questions...

Sect. 18) How do I manipulate bits in Java?

Java Programmer's FAQ - Part D
Use bytes, shorts, chars, ints or longs if you need to manipulate no more than 64 bits at once. Use ~ for NOT, & for AND, | for OR, and ^ for XOR. Beware that the precedence for & | and ^ is not intuitive; they have lower precedence than == and !=, so you must write: if ((a & 1) == 1) rather than: if (a & 1 == 1) You can also shift bits with the <<, >> and >>> operators; >> is a signed shift and >>> is an unsigned shift. See similar questions...

Sect. 18) How can I clone using serialization?

Java Programmer's FAQ - Part D
Look at the code below, submitted by expert programmer John Dumas. It uses serialization to write an object into a byte array, and reads it back to reconstitute a fresh copy. This is a clever hack! import java.io.ByteArrayOutputStream; import java.io.ByteArrayInputStream; import java.io.ObjectOutputStream; import java.io. See similar questions...

What is Hex?

FAQ - Sato Printers
Hex is just another way of representing a character. Instead of standard ASCII, which are your readable characters (Such as "A", "B", ")", etc.), Hex characters are displayed by a pair of characters that are limited to the characters of 0-9 and A-F. HOME | PRODUCTS | COMPANY CONTACTS | ABOUT US | TECH SUPPORT | REQUEST FOR QUOTE | CREDIT APPLICATION | ONLINE ORDER FORM | FREE CATALOG | SHIPMENT TRACKING | DIRECTIONS | E-MAIL | DISCOUNT INVENTORY See similar questions...

Sect. 18) Why can't I get String mutator methods to work?

Java Programmer's FAQ - Part D
Code like this seems to show that the calls don't work! String s = " hello "; s.trim(); s.toUpperCase(); Note again that Strings are immutable. This means that once a String has been initialized, its contents won't change. In the code above, the method calls return a different String with the desired alterations. But this new String is not assigned to anything, so the results are discarded. To see the changes, assign the results of the method call to the original String or to another String. See similar questions...

Sect. 18) How can you send a function pointer as an argument?

Java Programmer's FAQ - Part D
Simple answer: use a "callback". Make the parameter an interface and pass an argument instance that implements that interface. public interface CallShow { public void Show( ); } public class ShowOff implements CallShow { public void Show( ) { .... } public class ShowOff2 implements CallShow { public void Show( ) { .... } public class UseShow { CallShow savecallthis; UseShow( CallShow withthis ) { savecallthis = withthis; } void ReadyToShow( ) { savecallthis. See similar questions...

Sect. 18) How do I do I/O redirection using exec()?

Java Programmer's FAQ - Part D
This solution works on Unix platforms using either JDK 1.0.2, or JDK 1.1. The trick is to use an array of Strings for the command line: String[] command = {"/bin/sh", "-c", "/bin/ls > out.dat"}; If you don't do this, and simply use a single string, the shell will see the -c and /bin/ls and ignore everything else after that. It only expects a single argument after the -c. import java.io.*; import java.util. See similar questions...

Sect. 18) So why can't I exec common DOS commands this way (as in 18.8)?

Java Programmer's FAQ - Part D
The reason is that many of the DOS commands are not individual programs, but merely "functions" of command.com. There is no DIR.EXE or COPY.EXE for example. Instead, one executes the command processor (shell) explicitly with a request to perform the built-in command, like so: Runtime.getRuntime().exec("command.com /c dir") for example. On NT, the command interpreter is "cmd.exe", so the statement would be Runtime.getRuntime(). See similar questions...

Sect. 18) OK, how do I read the output of a command?

Java Programmer's FAQ - Part D
above (18.8, 18.9), adjusted like this: BufferedReader pOut= new BufferedReader( new InputStreamReader(p.getInputStream())); try { String s = pOut.readLine(); while (s != null) { System.out.println(s); s = pOut.readLine(); } } catch (IOException e) { } Another possibility is to read chunks of whatever length as they come in: ... p = r.exec(cmd); InputStream is = p.getInputStream(); int len; byte buf[] = new byte[1000]; try { while( (len = is. See similar questions...

Sect. 18) What is the point of creating the temporary reference to this.layoutMgr?

Java Programmer's FAQ - Part D
This code is from the 1.0 AWT, and the programmer was probably pretty skilled. public synchronized void layout() { LayoutManager layoutMgr = this.layoutMgr; if (layoutMgr != null) { layoutMgr.layoutContainer(this); } } The code makes a local copy of a global variable for one or both of two reasons. The first reason is that accessing local variables can be faster than accessing (non final) member variables. It's good for loops or where there are many references in the source. See similar questions...

Sect. 18) What is the difference between "a & b" and "a && b" ?

Java Programmer's FAQ - Part D
a & b" takes two boolean operands, or two integer operands. It always evaluates both operands. For booleans, it ANDs both operands together producing a boolean result. For integer types, it bitwise ANDs both operands together, producing a result that is the promoted type of the operands (i.e. long, or int). "|" is the corresponding bitwise OR operation. "^" is the corresponding bitwise XOR operation. a && b" is a "conditional AND" which only takes boolean operands. See similar questions...

Sect. 18) How can I get a globally unique ID in Java?

Java Programmer's FAQ - Part D
The only way in pure Java to create globally unique ids is to set up a server, accessible by all interested parties, which supplies the ids. There are classes in Java which may supply 'probably' unique ids, with varying levels of reliability --- but a dual processor machine with two JVMs running could easily generate duplicate ids. Note that a global server issuing a token (and periodic "are you still using it" messages) is a pretty good way to do cooperative file locking too. See similar questions...

Why does AsnOctets.toString() print a String, instead of a hex?

FAQ on Westhawk's SNMP stack
The problem is that the stack has no MIB knowledge, so the method AsnOctets.toString() does a best guess and tries to figure out whether the octets are printable or not. If you know the AsnOctets represent a DateAndTime use AsnOctets.toCalendar(), or a DisplayString use AsnOctets.toDisplayString(). In version 4_14 we have added the interface uk.co.westhawk.snmp.stack.AsnOctetsPrintableFace and the class uk.co.westhawk.snmp.stack. See similar questions...

What's the deal on sprintf's return value? Is it an int or a char *?

Stdio
The Standard says that it returns an int (the number of characters written, just like printf and fprintf). Once upon a time, in some C libraries, sprintf returned the char * value of its first argument, pointing to the completed result (i.e. analogous to strcpy's return value). See similar questions...

Can I print out a variable's value within a stored routine for debugging purposes?

Appendix A. Frequently Asked Questions About MySQL 5.1
Yes, you can do this in a stored procedure, but not in a stored function. If you perform an ordinary SELECT inside a stored procedure, the result set is returned directly to the client. You will need to use the MySQL 4.1 (or above) client-server protocol for this to work. This means that - for instance - in PHP, you need to use the mysqli extension rather than the old mysql extension. See similar questions...

Explore Other Topics

What is a betterment charge?
What are the usual nicotine withdrawal symptoms?
Is Publisher 2003 included in Microsoft Office 2003 Editions?
How can I kill aphids without also killing the caterpillars on my Milkweed?
Can I take ALEVE if I have Diabetes?
I accidently hid some of my friends... how do I unhide them?
How do I access my Outlook email from home?
Should I clean the Brine Tank?
How does GBIP define "Market"?
Do you make Anime costumes in larger sizes?
How do I get paid and what is the freight broker agent pay / salary?
What are the Rotary special monthly themes ?
How can I use Ghostscript to convert PS to PDF?
More Questions >>

© Copyright 2007-2012 QueryCAT
About • Webmasters • Contact