Casting Parameter Variables from int to char and vice-versa

inventorbc

If I pass a char as a parameter when calling stuff, will it compile? Will passing an int instead of a char work too? I know you can cast from int to char but shouldn't it be explicit?

I have a class Test with one static method of return type String with char a and int x as parameters.

public class Test {
    public static String stuff(char a, int x)
    {
        char b = (char)x; String s = "";
        while (a<b)
        s+=a+b--;
        return s;
    }
    public static void main(String args[])
    {
         System.out.println(stuff('a','d'));
    }
}
Hello World

Up-casting(implicit)-

byte –> short –> int –> long –> float –> double

Down-casting(Explicit)-

double –> float –> long –> int–> short–> byte

int takes 4 bytes of memory and char takes 2 bytes of memory, so requires explicit casting. int can take negative values and char takes only positive values.

      public class Test {
      public static void main(String args[]) { 
      int i = 78;     // 4 bytes 
      // char ch = i;   // error, 4 bytes to 2 bytes
      char ch = (char) i; // int is type converted to char
      System.out.println(ch); // prints N (78 ASCII value for N)

      System.out.println("Max int:"+Integer.SIZE+"bit");
      System.out.println("Max char:"+Character.SIZE+"bit");

       }
     } 

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Casting a bitset to unsigned char, and vice versa

Int to UInt (and vice versa) bit casting in Swift

variables from jquery to php and vice versa

Convert from hex to int and vice versa in java

Casting from string to uint8 and vice versa

C: Converting unsigned char array to signed int (vice versa)

Casting from `int` to `unsigned char`

How to convert from stringstream to unsigned char vector and vice versa?

Access form methods and variables from a class and vice versa in C#

SASS: Generating list of variables from list of classes (and vice versa)

Python - making and int from 8 boolean byte values and vice versa

Conversion from runes / int8 array to string and vice versa

Dartlang: How to convert int from network to host order (and vice versa)

Java - Converting String from Scanner to int and vice versa

Casting from int* to char* in c++

Is converting from unsigned char to signed char and vice versa in C89 well defined?

ByteBuffer - convert long to char array and vice versa

Converting char to binary strings and vice-versa

Convert List<int> to string and vice versa?

BitSet to Set[Int] or vice versa in Scala

How to convert Drawable into int and vice versa in Android

Casting Int as Unsigned Char

How to derive signed type from unsigned type generic parameter, and vice-versa?

Why casting from int, double, float and decimal to char works, but when those same variables are beforehand casted to IConvertible, doesn't?

How to stop Java from automatically casting a char value to an int?

Does casting from Char to Int always give positive values in C

How to move from UIViewController to SKView and vice versa?

Reading objects into array from file and vice versa

Access NSDocument from NSViewController and vice versa

TOP Ranking

HotTag

Archive