Java/Data Type/short
Содержание
- 1 Cast back to short
- 2 Cast result of plus operation to byte
- 3 Compare Two Java short Arrays
- 4 Convert Java String to Short
- 5 Convert Short to numeric primitive data types
- 6 Convert String to short primitive
- 7 Gets the maximum of three short values.
- 8 Gets the minimum of three short values.
- 9 Java Short Example
- 10 Java short: short is 16 bit signed type ranges from �32,768 to 32,767.
- 11 Java Sort short Array
- 12 Short class creates primitives that wrap themselves around data items of the short data type
- 13 Shorts Vs Ints
- 14 short value
- 15 Use Short constructor to convert short primitive type to Short object
- 16 Use toString method of Short class to convert Short into String.
Cast back to short
<source lang="java">
public class Main {
public static void main(String[] args) { short a, b, c; c = 2; b = 9; a = (short) (b + c); System.out.println("a is " + a); }
}
</source>
Cast result of plus operation to byte
<source lang="java">
public class Main {
public static void main(String[] args) { short a, b, c; c = 2; byte s; s = (byte) c; System.out.println("s is " + s); }
}
</source>
Compare Two Java short Arrays
<source lang="java">
import java.util.Arrays; public class Main {
public static void main(String[] args) { short[] a1 = new short[] { 1, 3, 8 }; short[] a2 = new short[] { 7, 3, 8 }; System.out.println(Arrays.equals(a1, a2)); }
}
</source>
Convert Java String to Short
<source lang="java">
public class Main {
public static void main(String[] args) { Short sObj1 = new Short("100"); System.out.println(sObj1); String str = "100"; Short sObj2 = Short.valueOf(str); System.out.println(sObj2); }
}
</source>
Convert Short to numeric primitive data types
<source lang="java">
public class Main {
public static void main(String[] args) { Short sObj = new Short("10"); byte b = sObj.byteValue(); System.out.println(b); short s = sObj.shortValue(); System.out.println(s); int i = sObj.intValue(); System.out.println(i); float f = sObj.floatValue(); System.out.println(f); double d = sObj.doubleValue(); System.out.println(d); long l = sObj.longValue(); System.out.println(l); }
} /* 10 10 10 10.0 10.0 10
- /
</source>
Convert String to short primitive
<source lang="java">
public class Main {
public static void main(String[] args) { short s = Short.parseShort("10"); System.out.println(s); }
}
</source>
Gets the maximum of three short values.
<source lang="java">
/*
* Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You under the Apache License, Version 2.0 * (the "License"); you may not use this file except in compliance with * the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */
/**
*Provides extra functionality for Java Number classes.
* * @author * @since 2.0 * @version $Id: NumberUtils.java 609475 2008-01-06 23:58:59Z bayard $ */
public class Main {
/***
Gets the maximum of three short
values.
* * @param a value 1 * @param b value 2 * @param c value 3 * @return the largest of the values */ public static short max(short a, short b, short c) { if (b > a) { a = b; } if (c > a) { a = c; } return a; }
}
</source>
Gets the minimum of three short values.
<source lang="java">
/*
* Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You under the Apache License, Version 2.0 * (the "License"); you may not use this file except in compliance with * the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */
/**
*Provides extra functionality for Java Number classes.
* * @author * @since 2.0 * @version $Id: NumberUtils.java 609475 2008-01-06 23:58:59Z bayard $ */
public class Main {
/***
Gets the minimum of three short
values.
* * @param a value 1 * @param b value 2 * @param c value 3 * @return the smallest of the values */ public static short min(short a, short b, short c) { if (b < a) { a = b; } if (c < a) { a = c; } return a; }
}
</source>
Java Short Example
<source lang="java">
public class Main {
public static void main(String[] args) { short s = 10; Short sObj1 = new Short(s); Short sObj2 = new Short("10"); System.out.println(sObj1); System.out.println(sObj2); }
}
</source>
Java short: short is 16 bit signed type ranges from �32,768 to 32,767.
<source lang="java">
public class Main {
public static void main(String[] args) { short s1 = 50; short s2 = 42; System.out.println("Value of short variable b1 is :" + s1); System.out.println("Value of short variable b1 is :" + s2); }
} /* Value of short variable b1 is :50 Value of short variable b1 is :42
- /
</source>
Java Sort short Array
<source lang="java">
import java.util.Arrays; public class Main {
public static void main(String[] args) { short[] s1 = new short[] { 31, 21, 51, 41, 11 }; for (short s : s1) { System.out.print(" " + s); } Arrays.sort(s1); for (short s : s1) { System.out.print(" " + s); } short[] s2 = new short[] { 5, 2, 3, 1, 4 }; Arrays.sort(s2, 1, 4); for (short s : s2) { System.out.print(" " + s); } }
}
</source>
Short class creates primitives that wrap themselves around data items of the short data type
<source lang="java">
public class MainClass {
public static void main(String[] args) { short s = -1800; Short s2 = new Short(s); System.out.println(s2.shortValue()); }
}
</source>
Shorts Vs Ints
<source lang="java">
public class ShortsVsInts {
public static void main(String[] unused) { short i,j; i = 30; j = ++i; // works j += 1; // works j += 32768; // compiles; truncates at run time! System.out.println(j); System.out.println(Short.MAX_VALUE); //j = j + 1; // won"t compile }
}
</source>
short value
<source lang="java">
public class Variables {
public static void main(String[] arguments) { final char UP = "U"; byte initialLevel = 12; short location = 13250; int score = 3500100; boolean newGame = true; System.out.println("Level: " + initialLevel); System.out.println("Up: " + UP); }
}
</source>
Use Short constructor to convert short primitive type to Short object
<source lang="java">
public class Main {
public static void main(String[] args) { short s = 10; Short sObj = new Short(s); System.out.println(sObj); }
}
</source>
Use toString method of Short class to convert Short into String.
<source lang="java">
public class Main {
public static void main(String[] args) { short s = 10; Short sObj = new Short(s); String str = sObj.toString(); System.out.println(str); }
}
</source>