Solved

char and int conversions...

Posted on 1997-06-30
2
298 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
Comment Utility
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
Comment Utility
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Safari, Firefox, IE, Java, Plugin, Java Security error 13 39
mapBully challenge 6 88
Modeling a class in java 5 31
@SBGen Method 3 25
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…
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

762 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now