Solved

char and int conversions...

Posted on 1997-06-30
2
302 Views
Last Modified: 2010-04-16
I need to do the following:

I have an integer with a value, and for this example we'll use 65.

Now, I want to convert that value to the character 65 or "A".

In C, I can use:
   int temp1=65;
   char temp2;

   temp2=temp1;

and boom, I have temp2 == 'A'

However, in Java I can't do that.  I can do:
  int temp1;
  char temp2='A';
 
  temp1=temp2;

and instantly temp1=65, but I can't go the other way.  Is there a way to do this?

Thank you.
0
Comment
Question by:zathael
2 Comments
 
LVL 2

Accepted Solution

by:
webster030697 earned 200 total points
ID: 1222523
Unlike C, Java is strongly typed (thankfully). To do what you want, you must expliciltly cast the data type:

import java.io.*;

public class TestChar
{
      
      public static void main(String Args[])
      {      int theInt = 65;
            char theChar;
            theChar = (char) theInt;
            System.out.println("theInt = " + theInt + "\n");
            System.out.println("theChar = " + theChar);
      }
      
}

produces the output:

theInt = 65

theChar = A

Why you would want to do this is another question. Strong typing is one of the great features of Java - you can't just go juxtaposing data types willy-nilly, causing all sorts of confusion in the process.
0
 

Author Comment

by:zathael
ID: 1222524
The reason I needed to do this is there are certain c routines that I'm taking to Java, certain functions that I find useful in my projects that work great in C, and now I need in Java.  One such function required this.  For the most part I can work around them, but rather than add 10 lines of code to do the same thing, I thought there might be a better way to take it to one line of code. Thank you for your answer.
0

Featured Post

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
simple java question 3 56
configure dependency in POM for new database 3 26
spring jars download 1 29
Android development question 2 38
An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
The viewer will learn how to implement Singleton Design Pattern in Java.

821 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question