Solved

char and int conversions...

Posted on 1997-06-30
2
304 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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Spring Framework HTTPSession management 1 45
servlet example issue 6 46
How to convert from xls to xlsx using java 7 56
mysql jsp example issue 32 51
For customizing the look of your lightweight component and making it look opaque like it was made of plastic.  This tip assumes your component to be of rectangular shape and completely opaque.   (CODE)
For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

820 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