Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.java.programmer > #24146 > unrolled thread

Conversions in Java

Started byPanoramix <lucasreis3000@gmail.com>
First post2013-05-21 08:23 -0700
Last post2013-05-22 03:04 -0700
Articles 14 — 9 participants

Back to article view | Back to comp.lang.java.programmer


Contents

  Conversions in Java Panoramix <lucasreis3000@gmail.com> - 2013-05-21 08:23 -0700
    Re: Conversions in Java Arne Vajhøj <arne@vajhoej.dk> - 2013-05-21 11:23 -0400
    Re: Conversions in Java Arved Sandstrom <asandstrom2@eastlink.ca> - 2013-05-21 18:09 -0300
    Re: Conversions in Java Lew <lewbloch@gmail.com> - 2013-05-21 14:49 -0700
    Re: Conversions in Java Panoramix <lucasreis3000@gmail.com> - 2013-05-21 15:07 -0700
      Re: Conversions in Java Eric Sosman <esosman@comcast-dot-net.invalid> - 2013-05-21 18:17 -0400
      Re: Conversions in Java Robert Klemme <shortcutter@googlemail.com> - 2013-05-25 23:27 +0200
        Re: Conversions in Java Arved Sandstrom <asandstrom2@eastlink.ca> - 2013-05-25 18:59 -0300
      Re: Conversions in Java Arne Vajhøj <arne@vajhoej.dk> - 2013-06-01 22:01 -0400
        Re: Conversions in Java Kevin McMurtrie <mcmurtrie@pixelmemory.us> - 2013-06-02 02:38 -0700
          Re: Conversions in Java Lew <lewbloch@gmail.com> - 2013-06-02 11:41 -0700
        Re: Conversions in Java Sven Köhler <remove-sven.koehler@gmail.com> - 2013-06-02 16:14 +0300
          Re: Conversions in Java Arne Vajhøj <arne@vajhoej.dk> - 2013-06-02 09:50 -0400
    Re: Conversions in Java Roedy Green <see_website@mindprod.com.invalid> - 2013-05-22 03:04 -0700

#24146 — Conversions in Java

FromPanoramix <lucasreis3000@gmail.com>
Date2013-05-21 08:23 -0700
SubjectConversions in Java
Message-ID<78f7af83-7e0e-4592-b1f0-2b6bd466feb5@googlegroups.com>
Someone has a good introduction to conversions of values in Java?

I am working in a project that I need to use this and I'm having some trouble about this.


Thanks!

[toc] | [next] | [standalone]


#24148

FromArne Vajhøj <arne@vajhoej.dk>
Date2013-05-21 11:23 -0400
Message-ID<519b9186$0$32104$14726298@news.sunsite.dk>
In reply to#24146
On 5/21/2013 11:23 AM, Panoramix wrote:
> Someone has a good introduction to conversions of values in Java?
>
> I am working in a project that I need to use this and I'm having some trouble about this.

What type of conversions?

Arne

[toc] | [prev] | [next] | [standalone]


#24157

FromArved Sandstrom <asandstrom2@eastlink.ca>
Date2013-05-21 18:09 -0300
Message-ID<TnRmt.11770$Ve3.9735@newsfe12.iad>
In reply to#24146
On 05/21/2013 12:34 PM, Stefan Ram wrote:
> Panoramix <lucasreis3000@gmail.com> writes:
>> Someone has a good introduction to conversions of values in Java?
>
>    There are no special books about the conversions of values
>    in Java, but the topic is treated in every good Java text book.

And the language spec and type APIs.

>> I am working in a project that I need to use this and I'm having some trouble about this.
>
>    Possibly, you might want
>
> java.lang.Double.valueOf( yourText )
>
Good point, both Arne and Stefan - it may not be simply implicit numeric 
type conversions referred to here, but also parsing string into numeric, 
etc etc.

AHS

[toc] | [prev] | [next] | [standalone]


#24158

FromLew <lewbloch@gmail.com>
Date2013-05-21 14:49 -0700
Message-ID<0fa48b19-f62f-4c36-9263-64a40668f789@googlegroups.com>
In reply to#24146
Stefan Ram wrote:
> Panoramix writes:
>> Someone has a good introduction to conversions of values in Java?

Arne asked "What type of conversions?" Here is some general information about various 
conversion types:

http://docs.oracle.com/javase/tutorial/java/data/converting.html
http://docs.oracle.com/javase/tutorial/java/data/autoboxing.html
http://docs.oracle.com/javase/tutorial/java/IandI/polymorphism.html
http://docs.oracle.com/javase/tutorial/essential/io/scanfor.html
http://docs.oracle.com/javase/tutorial/java/IandI/interfaceAsType.html

If you read the Java Language Specification (JLS) you will get the normative definitions 
for various conversions:
http://docs.oracle.com/javase/specs/jls/se7/html/jls-5.html

If you mean various formatting or string conversions, as Stefan conjectured, then you 
should look at the Javadocs for various formatting and string-ish types:
http://docs.oracle.com/javase/7/docs/api/java/lang/String.html#format(java.lang.String, java.lang.Object...)
 et al.
http://docs.oracle.com/javase/7/docs/api/java/util/Scanner.html
http://docs.oracle.com/javase/7/docs/api/java/text/Format.html
 and related types

>   There are no special books about the conversions of values
>   in Java, but the topic is treated in every good Java text book.
> 
>> I am working in a project that I need to use this and I'm having some trouble about this.
> 
>   Possibly, you might want
> 
> java.lang.Double.valueOf( yourText )

There are similar 'valueOf()' methods in the other primitive wrapper classes ('Integer', ...).

Also, GIYF.

-- 
Lew

[toc] | [prev] | [next] | [standalone]


#24159

FromPanoramix <lucasreis3000@gmail.com>
Date2013-05-21 15:07 -0700
Message-ID<dab470bf-74c9-4bab-8be6-3ae86c527a50@googlegroups.com>
In reply to#24146
Thanks guys!

I really appreciate.

When I say conversions, I mean every type of conversion!

Thanks for the help

[toc] | [prev] | [next] | [standalone]


#24160

FromEric Sosman <esosman@comcast-dot-net.invalid>
Date2013-05-21 18:17 -0400
Message-ID<kngri5$4l6$1@dont-email.me>
In reply to#24159
On 5/21/2013 6:07 PM, Panoramix wrote:
> Thanks guys!
>
> I really appreciate.
>
> When I say conversions, I mean every type of conversion!

     There's a reasonably accurate summary at

<http://docs.oracle.com/javase/specs/jls/se7/html/jls-5.html>

> Thanks for the help

     Glad to be of service.

-- 
Eric Sosman
esosman@comcast-dot-net.invalid

[toc] | [prev] | [next] | [standalone]


#24180

FromRobert Klemme <shortcutter@googlemail.com>
Date2013-05-25 23:27 +0200
Message-ID<b0colgFc8vjU1@mid.individual.net>
In reply to#24159
On 22.05.2013 00:07, Panoramix wrote:

> When I say conversions, I mean every type of conversion!

Well, then you just need

public interface Converter<F,T> {
   T convert(F fromValue);
}

implement all conversions needed plus a smart mechanism to choose the 
appropriate one in every case. ;-)

Cheers

	robert

-- 
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/

[toc] | [prev] | [next] | [standalone]


#24181

FromArved Sandstrom <asandstrom2@eastlink.ca>
Date2013-05-25 18:59 -0300
Message-ID<Kuaot.9704$9X5.6186@newsfe31.iad>
In reply to#24180
On 05/25/2013 06:27 PM, Robert Klemme wrote:
> On 22.05.2013 00:07, Panoramix wrote:
>
>> When I say conversions, I mean every type of conversion!
>
> Well, then you just need
>
> public interface Converter<F,T> {
>    T convert(F fromValue);
> }
>
> implement all conversions needed plus a smart mechanism to choose the
> appropriate one in every case. ;-)
>
> Cheers
>
>      robert
>
JSF javax.faces.convert.Converter. :-) You've got to do the custom work 
and the mechanism is explicit, but still...

A fair few ESBs (enterprise service buses) also have what they call 
transformers (converters) and often a registration/auto-discovery 
mechanism. For example, in Java-based ESBs - which is most of them - you 
might have a message which encounters a processing component, which by 
entry point resolution, expects a payload of class X and to return a 
payload of class Y (null is acceptable): the ESB will consult a list of 
X<->Y converters that it knows about, and select the best converter 
according to a rule set.

AHS
-- 
When a true genius appears, you can know him by this sign:
that all the dunces are in a confederacy against him.
-- Jonathan Swift

[toc] | [prev] | [next] | [standalone]


#24248

FromArne Vajhøj <arne@vajhoej.dk>
Date2013-06-01 22:01 -0400
Message-ID<51aaa795$0$32106$14726298@news.sunsite.dk>
In reply to#24159
On 5/21/2013 6:07 PM, Panoramix wrote:
> When I say conversions, I mean every type of conversion!

Numbers of possible types are infinite.

So number of conversions between types are also infinite.

But if you are only thinking about the most basic types
then see below for some examples.

Arne

====

    int iv = 123;
    double xv2;
    xv2 = iv;

    int iv = 123;
    boolean bv2;
    bv2 = (iv != 0);

    int iv = 123;
    char cv2;
    cv2 = (char)iv;

    int iv = 123;
    String sv2;
    sv2 = Integer.toString(iv);

    double xv = 123.456;
    int iv2;
    iv2 = (int)xv;

    double xv = 123.456;
    String sv2;
    sv2 = Double.toString(xv);

    boolean bv = true;
    int iv2;
    iv2 = (bv ? 1 : 0);

    boolean bv = true;
    String sv2;
    sv2 = (new Boolean(bv)).toString();

    char cv = 'A';
    int iv2;
    iv2 = cv;

    char cv = 'A';
    String sv2;
    sv2 = new String(new char[] { cv });

    String sv = "123";
    int iv2;
    iv2 = Integer.parseInt(sv);

    String sv = "123.456";
    double xv2;
    xv2 = Double.parseDouble(sv);

    String sv = "true";
    boolean bv2;
    bv2 = Boolean.valueOf(sv).booleanValue();

    String sv = "ABC";
    char cv2;
    cv2 = sv.charAt(0);

    int iv = 123;
    String sv2;
    sv2 = Integer.toHexString(iv);

    String sv = "7b";
    int iv2;
    iv2 = Integer.parseInt(sv, 16);

    byte[] b = { 65, 66, 67 };
    String s2;
    s2 = new String(b, "ISO-8859-1");

    String s = "abc";
    byte[] b2;
    b2 = s.getBytes("ISO-8859-1");

    int iv = 123;
    Integer iv2;
    iv2 = new Integer(iv);

    Integer iv = new Integer(123);
    int iv2;
    iv2 = iv.intValue();

    java.util.Date d;
    java.util.Calendar cal = new java.util.GregorianCalendar();
    d = cal.getTime();

    java.util.Date d = new java.util.Date();
    java.util.Calendar cal = new java.util.GregorianCalendar();
    cal.setTime(d);

    java.util.Date d = new java.util.Date();
    java.text.DateFormat df = new 
java.text.SimpleDateFormat("dd-MMM-yyyy hh:mm");
    String ds;
    ds = df.format(d);

    String ds = "31-Jan-2004 09:24"
    java.util.Date d;
    java.text.DateFormat df = new 
java.text.SimpleDateFormat("dd-MMM-yyyy hh:mm");
    d = df.parse(ds);

    java.util.Date d = new java.util.Date();
    java.sql.Timestamp ts;
    ts = new java.sql.Timestamp(d.getTime());

    java.sql.Timestamp ts = new java.sql.Timestamp((new Date()).getTime());
    java.util.Date d;
    d = ts;

[toc] | [prev] | [next] | [standalone]


#24255

FromKevin McMurtrie <mcmurtrie@pixelmemory.us>
Date2013-06-02 02:38 -0700
Message-ID<51ab1293$0$52757$742ec2ed@news.sonic.net>
In reply to#24248
Don't forget shorthand operators that automatically typecast:

 +=
 -=
 *=
 /=

  int a= 0;
  a+= Double.MAX_VALUE;
  a+= Float.MAX_VALUE;
  a+= Long.MAX_VALUE;

The shorthand bit operators automatically typecast length:

 |=
 &=
 ^=

  a|= Long.MAX_VALUE;



In article <51aaa795$0$32106$14726298@news.sunsite.dk>,
 Arne Vajhøj <arne@vajhoej.dk> wrote:

> On 5/21/2013 6:07 PM, Panoramix wrote:
> > When I say conversions, I mean every type of conversion!
> 
> Numbers of possible types are infinite.
> 
> So number of conversions between types are also infinite.
> 
> But if you are only thinking about the most basic types
> then see below for some examples.
> 
> Arne
> 
> ====
> 
>     int iv = 123;
>     double xv2;
>     xv2 = iv;
> 
>     int iv = 123;
>     boolean bv2;
>     bv2 = (iv != 0);
> 
>     int iv = 123;
>     char cv2;
>     cv2 = (char)iv;
> 
>     int iv = 123;
>     String sv2;
>     sv2 = Integer.toString(iv);
> 
>     double xv = 123.456;
>     int iv2;
>     iv2 = (int)xv;
> 
>     double xv = 123.456;
>     String sv2;
>     sv2 = Double.toString(xv);
> 
>     boolean bv = true;
>     int iv2;
>     iv2 = (bv ? 1 : 0);
> 
>     boolean bv = true;
>     String sv2;
>     sv2 = (new Boolean(bv)).toString();
> 
>     char cv = 'A';
>     int iv2;
>     iv2 = cv;
> 
>     char cv = 'A';
>     String sv2;
>     sv2 = new String(new char[] { cv });
> 
>     String sv = "123";
>     int iv2;
>     iv2 = Integer.parseInt(sv);
> 
>     String sv = "123.456";
>     double xv2;
>     xv2 = Double.parseDouble(sv);
> 
>     String sv = "true";
>     boolean bv2;
>     bv2 = Boolean.valueOf(sv).booleanValue();
> 
>     String sv = "ABC";
>     char cv2;
>     cv2 = sv.charAt(0);
> 
>     int iv = 123;
>     String sv2;
>     sv2 = Integer.toHexString(iv);
> 
>     String sv = "7b";
>     int iv2;
>     iv2 = Integer.parseInt(sv, 16);
> 
>     byte[] b = { 65, 66, 67 };
>     String s2;
>     s2 = new String(b, "ISO-8859-1");
> 
>     String s = "abc";
>     byte[] b2;
>     b2 = s.getBytes("ISO-8859-1");
> 
>     int iv = 123;
>     Integer iv2;
>     iv2 = new Integer(iv);
> 
>     Integer iv = new Integer(123);
>     int iv2;
>     iv2 = iv.intValue();
> 
>     java.util.Date d;
>     java.util.Calendar cal = new java.util.GregorianCalendar();
>     d = cal.getTime();
> 
>     java.util.Date d = new java.util.Date();
>     java.util.Calendar cal = new java.util.GregorianCalendar();
>     cal.setTime(d);
> 
>     java.util.Date d = new java.util.Date();
>     java.text.DateFormat df = new 
> java.text.SimpleDateFormat("dd-MMM-yyyy hh:mm");
>     String ds;
>     ds = df.format(d);
> 
>     String ds = "31-Jan-2004 09:24"
>     java.util.Date d;
>     java.text.DateFormat df = new 
> java.text.SimpleDateFormat("dd-MMM-yyyy hh:mm");
>     d = df.parse(ds);
> 
>     java.util.Date d = new java.util.Date();
>     java.sql.Timestamp ts;
>     ts = new java.sql.Timestamp(d.getTime());
> 
>     java.sql.Timestamp ts = new java.sql.Timestamp((new Date()).getTime());
>     java.util.Date d;
>     d = ts;
-- 
I will not see posts from Google because I must filter them as spam

[toc] | [prev] | [next] | [standalone]


#24269

FromLew <lewbloch@gmail.com>
Date2013-06-02 11:41 -0700
Message-ID<6a5ea732-7b16-44c2-bf78-17bcd6e3d71e@googlegroups.com>
In reply to#24255
Kevin McMurtrie wrote:
> Don't forget shorthand operators that automatically typecast:

The JLS doesn't call these "typecasts", it calls them "widening 
and narrowing conversions", in case you're looking for the rules 
at the source.

>  +=
>  -=
>  *=
>  /=
> 
>   int a= 0;
>   a+= Double.MAX_VALUE;
>   a+= Float.MAX_VALUE;
>   a+= Long.MAX_VALUE;
> 
> The shorthand bit operators automatically typecast length:

This is the same conversion as with += and the rest.

>  |=
>  &=
>  ^=
> 
>   a|= Long.MAX_VALUE;
> 
> In article <51aaa795$0$32106$14726298@news.sunsite.dk>,

And please do not top-post.

>  Arne Vajhøj <arne@vajhoej.dk> wrote:
>> Panoramix wrote:

-- 
Lew

[toc] | [prev] | [next] | [standalone]


#24256

FromSven Köhler <remove-sven.koehler@gmail.com>
Date2013-06-02 16:14 +0300
Message-ID<b10upgFprprU1@mid.dfncis.de>
In reply to#24248
On 06/02/2013 05:01 AM, Arne Vajhøj wrote:
> On 5/21/2013 6:07 PM, Panoramix wrote:
>> When I say conversions, I mean every type of conversion!
>
> Numbers of possible types are infinite.
>
> So number of conversions between types are also infinite.

But the formal description of all possible conversions between types is 
not infinite.


Regards,
   Sven

[toc] | [prev] | [next] | [standalone]


#24259

FromArne Vajhøj <arne@vajhoej.dk>
Date2013-06-02 09:50 -0400
Message-ID<51ab4da7$0$32104$14726298@news.sunsite.dk>
In reply to#24256
On 6/2/2013 9:14 AM, Sven Köhler wrote:
> On 06/02/2013 05:01 AM, Arne Vajhøj wrote:
>> On 5/21/2013 6:07 PM, Panoramix wrote:
>>> When I say conversions, I mean every type of conversion!
>>
>> Numbers of possible types are infinite.
>>
>> So number of conversions between types are also infinite.
>
> But the formal description of all possible conversions between types is
> not infinite.

True.

But if someone want to ask that question and can understand the answer,
then usually they don't need to ask in the first place.

Arne

[toc] | [prev] | [next] | [standalone]


#24165

FromRoedy Green <see_website@mindprod.com.invalid>
Date2013-05-22 03:04 -0700
Message-ID<f06pp8d4s1b12msnnt7njrcd2312gec4mg@4ax.com>
In reply to#24146
On Tue, 21 May 2013 08:23:13 -0700 (PDT), Panoramix
<lucasreis3000@gmail.com> wrote, quoted or indirectly quoted someone
who said :

>Someone has a good introduction to conversions of values in Java?

see http://mindprod.com/applet/converter.html
-- 
Roedy Green Canadian Mind Products http://mindprod.com
Technological possibilities are irresistible to man. 
If man can go to the moon, he will. 
If he can control the climate, he will. 
 ~ John von Neumann (born: 1903-12-28 died: 1957-02-08 at age: 53)

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.java.programmer


csiph-web