BvanW BvanW -4 years ago 62
Java Question

What is the maximum char value in a java program in Netbeans IDE/ what is wrong with my program?

What is the maximum Unicode value of a char in Java (in particular in the Netbeans IDE, if that makes any difference) I've been trying to write a program that, as part of the program, multiplies a char by a random number. According to what I've heard, based on the maximum Unicode value I should be able to multiply the highest value char I'm using (the tilde) by at least 8000 without causing overflow, however overflow does occur in my program. Is there a difference between the maximum Unicode char value and the maximum that is available in Netbeans? In case that isn't the case I have included my code below:

public static void main(String[] args) {

String pass = "Password";
String pwE = "";
int key [] = new int[pass.length()];

for (int i = 0; i < pass.length(); i++)
key[i] = 500;

for (int i = 0; i < pass.length(); i++)
pwE += (char)(pass.charAt(i)*key[i]);
pass = "";

for (int i = 0; i < pwE.length(); i++)
pass += (char)(pwE.charAt(i)/key[i]);

Answer Source

A char is a 16 bit unsigned type in Java.

Its maximum value is 65535.

Your multiplication of a char by an element of key looks suspect to me. Your casting this result (which will be an int type) back to char causes wraparound modulo 65536.

Your suspecting Netbeans is a red herring.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download