Beruflich Dokumente
Kultur Dokumente
Data Type
2. 1. 4. Literals
Print the limits of primitive types (e.g. byte, short, int ...) in
2. 1. 7.
Java
Java has eight primitive types of data: byte, short, int, long, char, float, double, and boolean.
byte
int
long
Type Explanation
boolean false
byte 0
short 0
int 0
long 0L
char \u0000
float 0.0f
double 0.0d
2. 1. 4. Literals
long n3 = 200;
big = 2147483647
bigger = -4
2. 1. 7. Print the limits of primitive types (e.g. byte, short, int ...) in Java
char c;
byte b;
short s;
int i;
long l;
float f;
double d;
void print(String s) {
System.out.println(s);
}
void printInitialValues() {
print("Data type Initial value");
print("boolean " + t);
print("char [" + c + "]");
print("byte " + b);
print("short " + s);
print("int " + i);
print("long " + l);
print("float " + f);
print("double " + d);
}
/*
* JBoss, Home of Professional Open Source
* Copyright 2005, JBoss Inc., and individual contributors as indicated
* by the @authors tag. See the copyright.txt in the distribution for a
* full listing of individual contributors.
*
* This is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation; either version 2.1 of
* the License, or (at your option) any later version.
*
* This software is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this software; if not, write to the Free
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
*/
/**
* Primitive utilities.
*
* @version <tt>$Revision: 1958 $</tt>
* @author <a href="mailto:jason@planet57.com">Jason Dillon</a>
*/
public final class Primitives {
/**
* Get a Boolean from a boolean, equivalent to the java 1.4 method
* Boolean.valueOf(boolean)
*
* @param value
* the boolean
* @return the Boolean equivalent
*/
public static Boolean valueOf(boolean value) {
if (value)
return Boolean.TRUE;
else
return Boolean.FALSE;
}
/**
* Test the equality of two doubles by converting their values into IEEE 754
* floating-point "double format" long values.
*
* @param a
* Double to check equality with.
* @param b
* Double to check equality with.
* @return True if a equals b.
*/
public static boolean equals(final double a, final double b) {
return Double.doubleToLongBits(a) == Double.doubleToLongBits(b);
}
/**
* Test the equality of two doubles by converting their values into IEEE 754
* floating-point "single precision" bit layouts.
*
* @param a
* Float to check equality with.
* @param b
* Float to check equality with.
* @return True if a equals b.
*/
public static boolean equals(final float a, final float b) {
return Float.floatToIntBits(a) == Float.floatToIntBits(b);
}
/**
* Test the equality of a given sub-section of two byte arrays.
*
* @param a
* The first byte array.
* @param abegin
* The begining index of the first byte array.
* @param b
* The second byte array.
* @param bbegin
* The begining index of the second byte array.
* @param length
* The length of the sub-section.
* @return True if sub-sections are equal.
*/
public static boolean equals(final byte a[], final int abegin, final byte b[], final int bbegi
final int length) {
try {
int i = length;
while (--i >= 0) {
if (a[abegin + i] != b[bbegin + i]) {
return false;
}
}
} catch (ArrayIndexOutOfBoundsException e) {
return false;
}
return true;
}
/**
* Test the equality of two byte arrays.
*
* @param a
* The first byte array.
* @param b
* The second byte array.
* @return True if the byte arrays are equal.
*/
public static boolean equals(final byte a[], final byte b[]) {
if (a == b)
return true;
if (a == null || b == null)
return false;
if (a.length != b.length)
return false;
try {
for (int i = 0; i < a.length; i++) {
if (a[i] != b[i]) {
return false;
}
}
} catch (ArrayIndexOutOfBoundsException e) {
return false;
}
return true;
}
import java.util.HashSet;
import java.util.List;
import java.util.Set;
/*
* JBoss, Home of Professional Open Source
* Copyright 2005, JBoss Inc., and individual contributors as indicated
* by the @authors tag. See the copyright.txt in the distribution for a
* full listing of individual contributors.
*
* This is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation; either version 2.1 of
* the License, or (at your option) any later version.
*
* This software is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this software; if not, write to the Free
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
*/
2. 2. Boolean
2. 2. 1. java.lang.Boolean
2. 2. 4. Boolean Literals
2. 2. 5. Boolean Variables
2. 2. 1. java.lang.Boolean
The java.lang.Boolean class wraps a boolean. You can construct a Boolean object from a boolean or a String
using one of these constructors.
public Boolean (boolean value)
public Boolean (String value)
For example:
Boolean b1 = new Boolean (false);
Boolean b2 = new Boolean ("true");
To convert a Boolean to a boolean, use its booleanValue method: public boolean booleanValue()
public class MainClass {
System.out.println(b1.booleanValue());
System.out.println(b2.booleanValue());
}
false
true
int x = 10;
boolean y = (x > 15);
System.out.println("y is " + y);
}
}
2. 2. 4. Boolean Literals
The boolean type has two values, represented by literals 'true' and 'false'.
The following code declares a boolean variable includeSign and assigns it the value of true.
public class MainClass {
System.out.println(includeSign);
}
2. 2. 5. Boolean Variables
1. Variables of type boolean can have only one of two values, true or false.
2. The values 'true' and 'false' are boolean literals.
3.
false
b = false;
System.out.println("b is " + b);
b = true;
System.out.println("b is " + b);
b = false;
if(b) System.out.println("This is not executed.");
b is false
b is true
This is executed.
10 > 9 is true
The static method valueOf parses a String to a Boolean object: public static Boolean valueOf (String string)
public class MainClass {
true
true
System.out.println(theValue);
}
}
//true
System.out.println(theValueAsString);
}
}
//true
import java.util.Arrays;
System.out.println(b);
}
}
//true
if (bool.equals(Boolean.TRUE)) {
System.out.println("bool = " + bool);
}
String s = "false";
String f = "abc";
Boolean abc = Boolean.valueOf(f);
System.out.println("abc = " + abc);
}
}
}
}
class AutoBox5 {
public static void main(String args[]) {
Boolean b = true;
if (b)
System.out.println("b is true");
/*
* 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.
*/
/**
* Operations on boolean primitives and Boolean objects.
*
* This class tries to handle <code>null</code> input gracefully.
* An exception will not be thrown for a <code>null</code> input.
* Each method documents its behaviour in more detail.
*
* @author Stephen Colebourne
* @author Matthew Hawthorne
* @author Gary Gregory
* @since 2.0
* @version $Id: BooleanUtils.java 589050 2007-10-27 05:07:45Z bayard $
*/
public class Main {
/**
* Converts a String to a Boolean.
*
* <code>'true'</code>, <code>'on'</code> or <code>'yes'</code>
* (case insensitive) will return <code>true</code>.
* <code>'false'</code>, <code>'off'</code> or <code>'no'</code>
* (case insensitive) will return <code>false</code>.
* Otherwise, <code>null</code> is returned.</p>
*
* <pre>
* BooleanUtils.toBooleanObject(null) = null
* BooleanUtils.toBooleanObject("true") = Boolean.TRUE
* BooleanUtils.toBooleanObject("false") = Boolean.FALSE
* BooleanUtils.toBooleanObject("on") = Boolean.TRUE
* BooleanUtils.toBooleanObject("ON") = Boolean.TRUE
* BooleanUtils.toBooleanObject("off") = Boolean.FALSE
* BooleanUtils.toBooleanObject("oFf") = Boolean.FALSE
* BooleanUtils.toBooleanObject("blue") = null
* </pre>
*
* @param str the String to check
* @return the Boolean value of the string,
* <code>null</code> if no match or <code>null</code> input
*/
public static Boolean toBooleanObject(String str) {
if ("true".equalsIgnoreCase(str)) {
return Boolean.TRUE;
} else if ("false".equalsIgnoreCase(str)) {
return Boolean.FALSE;
} else if ("on".equalsIgnoreCase(str)) {
return Boolean.TRUE;
} else if ("off".equalsIgnoreCase(str)) {
return Boolean.FALSE;
} else if ("yes".equalsIgnoreCase(str)) {
return Boolean.TRUE;
} else if ("no".equalsIgnoreCase(str)) {
return Boolean.FALSE;
}
// no match
return null;
}
}
/*
* 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.
*/
/**
* Operations on boolean primitives and Boolean objects.
*
* This class tries to handle <code>null</code> input gracefully.
* An exception will not be thrown for a <code>null</code> input.
* Each method documents its behaviour in more detail.
*
* @author Stephen Colebourne
* @author Matthew Hawthorne
* @author Gary Gregory
* @since 2.0
* @version $Id: BooleanUtils.java 589050 2007-10-27 05:07:45Z bayard $
*/
public class Main {
/**
* Converts a boolean to a String returning <code>'yes'</code>
* or <code>'no'</code>.
*
* <pre>
* BooleanUtils.toStringYesNo(true) = "yes"
* BooleanUtils.toStringYesNo(false) = "no"
* </pre>
*
* @param bool the Boolean to check
* @return <code>'yes'</code>, <code>'no'</code>,
* or <code>null</code>
*/
public static String toStringYesNo(boolean bool) {
return toString(bool, "yes", "no");
}
/**
* Converts a boolean to a String returning one of the input Strings.
*
* <pre>
* BooleanUtils.toString(true, "true", "false") = "true"
* BooleanUtils.toString(false, "true", "false") = "false"
* </pre>
*
* @param bool the Boolean to check
* @param trueString the String to return if <code>true</code>,
* may be <code>null</code>
* @param falseString the String to return if <code>false</code>,
* may be <code>null</code>
* @return one of the two input Strings
*/
public static String toString(boolean bool, String trueString, String falseString) {
return bool ? trueString : falseString;
}
}
/*
* 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.
*/
/**
* Operations on boolean primitives and Boolean objects.
*
* This class tries to handle <code>null</code> input gracefully.
* An exception will not be thrown for a <code>null</code> input.
* Each method documents its behaviour in more detail.
*
* @author Stephen Colebourne
* @author Matthew Hawthorne
* @author Gary Gregory
* @since 2.0
* @version $Id: BooleanUtils.java 589050 2007-10-27 05:07:45Z bayard $
*/
public class Main {
/**
* Converts an Integer to a boolean specifying the conversion values.
*
* <pre>
* BooleanUtils.toBoolean(new Integer(0), new Integer(1), new Integer(0)) = false
* BooleanUtils.toBoolean(new Integer(1), new Integer(1), new Integer(0)) = true
* BooleanUtils.toBoolean(new Integer(2), new Integer(1), new Integer(2)) = false
* BooleanUtils.toBoolean(new Integer(2), new Integer(2), new Integer(0)) = true
* BooleanUtils.toBoolean(null, null, new Integer(0)) = true
* </pre>
*
* @param value the Integer to convert
* @param trueValue the value to match for <code>true</code>,
* may be <code>null</code>
* @param falseValue the value to match for <code>false</code>,
* may be <code>null</code>
* @return <code>true</code> or <code>false</code>
* @throws IllegalArgumentException if no match
*/
public static boolean toBoolean(Integer value, Integer trueValue, Integer falseValue) {
if (value == null) {
if (trueValue == null) {
return true;
} else if (falseValue == null) {
return false;
}
} else if (value.equals(trueValue)) {
return true;
} else if (value.equals(falseValue)) {
return false;
}
// no match
throw new IllegalArgumentException("The Integer did not match either specified value")
;
}
}
/**
* Operations on boolean primitives and Boolean objects.
*
* This class tries to handle <code>null</code> input gracefully.
* An exception will not be thrown for a <code>null</code> input.
* Each method documents its behaviour in more detail.
*
* @author Stephen Colebourne
* @author Matthew Hawthorne
* @author Gary Gregory
* @since 2.0
* @version $Id: BooleanUtils.java 589050 2007-10-27 05:07:45Z bayard $
*/
public class Main {
/**
* Converts an int to a boolean specifying the conversion values.
*
* <pre>
* BooleanUtils.toBoolean(0, 1, 0) = false
* BooleanUtils.toBoolean(1, 1, 0) = true
* BooleanUtils.toBoolean(2, 1, 2) = false
* BooleanUtils.toBoolean(2, 2, 0) = true
* </pre>
*
* @param value the Integer to convert
* @param trueValue the value to match for <code>true</code>
* @param falseValue the value to match for <code>false</code>
* @return <code>true</code> or <code>false</code>
* @throws IllegalArgumentException if no match
*/
public static boolean toBoolean(int value, int trueValue, int falseValue) {
if (value == trueValue) {
return true;
} else if (value == falseValue) {
return false;
}
// no match
throw new IllegalArgumentException("The Integer did not match either specified value")
;
}
}
/**
* Operations on boolean primitives and Boolean objects.
*
* This class tries to handle <code>null</code> input gracefully.
* An exception will not be thrown for a <code>null</code> input.
* Each method documents its behaviour in more detail.
*
* @author Stephen Colebourne
* @author Matthew Hawthorne
* @author Gary Gregory
* @since 2.0
* @version $Id: BooleanUtils.java 589050 2007-10-27 05:07:45Z bayard $
*/
public class Main {
/**
* Performs an xor on a set of booleans.
*
* <pre>
* BooleanUtils.xor(new boolean[] { true, true }) = false
* BooleanUtils.xor(new boolean[] { false, false }) = false
* BooleanUtils.xor(new boolean[] { true, false }) = true
* </pre>
*
* @param array an array of <code>boolean<code>s
* @return <code>true</code> if the xor is successful.
* @throws IllegalArgumentException if <code>array</code> is <code>null</code>
* @throws IllegalArgumentException if <code>array</code> is empty.
*/
public static boolean xor(boolean[] array) {
// Validates input
if (array == null) {
throw new IllegalArgumentException("The Array must not be null");
} else if (array.length == 0) {
throw new IllegalArgumentException("Array is empty");
}
2. 3. 2. Integer Calculations
2. 3. 2. Integer Calculations
1. The basic operators in calculations are +, -, *, and /. They have the usual meanings: add, subtract,
multiply, and divide, respectively.
2. Using parentheses in arithmetic calculations to change the sequence of operations.
System.out.println(a);
System.out.println(b);
}
-34
8
import java.io.File;
/*
* 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.
*
*
*/
public class Main {
/**
* Add two integers, checking for overflow.
*
* @param x an addend
* @param y an addend
* @return the sum <code>x+y</code>
* @throws ArithmeticException if the result can not be represented as an
* int
* @since 1.1
*/
public static int addAndCheck(int x, int y) {
long s = (long)x + (long)y;
if (s < Integer.MIN_VALUE || s > Integer.MAX_VALUE) {
throw new ArithmeticException("overflow: add");
}
return (int)s;
}
}
2. 3. 4. Multiply two integers, checking for overflow.
import java.io.File;
/*
* 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.
*
*
*/
public class Main {
/**
* Multiply two integers, checking for overflow.
*
* @param x a factor
* @param y a factor
* @return the product <code>x*y</code>
* @throws ArithmeticException if the result can not be represented as an
* int
* @since 1.1
*/
public static int mulAndCheck(int x, int y) {
long m = ((long)x) * ((long)y);
if (m < Integer.MIN_VALUE || m > Integer.MAX_VALUE) {
throw new ArithmeticException("overflow: mul");
}
return (int)m;
}
}
import java.math.BigDecimal;
/*
* 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.
*
*
*/
public class Main {
/**
* Subtract two integers, checking for overflow.
*
* @param x the minuend
* @param y the subtrahend
* @return the difference <code>x-y</code>
* @throws ArithmeticException if the result can not be represented as an
* int
* @since 1.1
*/
public static int subAndCheck(int x, int y) {
long s = (long)x - (long)y;
if (s < Integer.MIN_VALUE || s > Integer.MAX_VALUE) {
throw new ArithmeticException("overflow: subtract");
}
return (int)s;
}
}
Binary Decimal
0000 0000 0
1000 0000 128
0000 0001 1
1000 0001 129
0000 0010 2
1000 0010 130
0001 0000 16
1001 0000 144
0001 0001 17
1001 0001 145
0111 1100 124
1111 1100 252
0111 1101 125
1111 1101 253
0111 1110 126
1111 1110 254
0111 1111 127
1111 1111 255
2. 3. 7. Min and Max values of datatype int
import java.math.BigDecimal;
import java.math.BigInteger;
/**
* 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 <a href="mailto:rand_mcneely@yahoo.com">Rand McNeely</a>
* @author Stephen Colebourne
* @author <a href="mailto:steve.downey@netfolio.com">Steve Downey</a>
* @author Eric Pugh
* @author Phil Steitz
* @since 1.0
* @version $Id: NumberUtils.java 488819 2006-12-19 21:50:04Z bayard $
*
*/
public class Main {
/**
* Gets the maximum of three <code>int</code> values.
*
* @param a value 1
* @param b value 2
* @param c value 3
* @return the largest of the values
*/
public static int maximum(int a, int b, int c) {
if (b > a) {
a = b;
}
if (c > a) {
a = c;
}
return a;
}
}
import java.math.BigDecimal;
import java.math.BigInteger;
/**
* 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 <a href="mailto:rand_mcneely@yahoo.com">Rand McNeely</a>
* @author Stephen Colebourne
* @author <a href="mailto:steve.downey@netfolio.com">Steve Downey</a>
* @author Eric Pugh
* @author Phil Steitz
* @since 1.0
* @version $Id: NumberUtils.java 488819 2006-12-19 21:50:04Z bayard $
*
*/
public class Main {
/**
* Gets the minimum of three <code>int</code> values.
*
* @param a value 1
* @param b value 2
* @param c value 3
* @return the smallest of the values
*/
public static int minimum(int a, int b, int c) {
if (b < a) {
a = b;
}
if (c < a) {
a = c;
}
return a;
}
}
/**
* 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.
*/
import java.io.PrintWriter;
import java.io.StringWriter;
import java.net.InetAddress;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.UnknownHostException;
import java.text.DateFormat;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.StringTokenizer;
import java.util.Collection;
/**
* General string utils
*/
public class StringUtils {
/**
* Given an integer, return a string that is in an approximate, but human
* readable format.
* It uses the bases 'k', 'm', and 'g' for 1024, 1024**2, and 1024**3.
* @param number the number to format
* @return a human readable form of the integer
*/
public static String humanReadableInt(long number) {
long absNumber = Math.abs(number);
double result = number;
String suffix = "";
if (absNumber < 1024) {
// nothing
} else if (absNumber < 1024 * 1024) {
result = number / 1024.0;
suffix = "k";
} else if (absNumber < 1024 * 1024 * 1024) {
result = number / (1024.0 * 1024);
suffix = "m";
} else {
result = number / (1024.0 * 1024 * 1024);
suffix = "g";
}
return oneDecimal.format(result) + suffix;
}
}
/*
* Permission is hereby granted, free of charge, to any person obtaining a copy of
* this software and associated documentation files (the "Software"), to deal in
* the Software without restriction, including without limitation the rights to
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
* of the Software, and to permit persons to whom the Software is furnished to do
* so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
2. 4. Byte
Java byte: byte is smallest Java integer type.byte is 8 bit signed type
2. 4. 1.
ranges from �128 to 127.
2. 4. 3. Convert byte to String: Using the static toString method of the Byt
class
public class Main {
public static void main(String[] args) {
byte b = 65;
System.out.println(Byte.toString(b));
}
}
System.out.println(b + "");
}
}
//65
}
}
//A
byte b = Byte.valueOf(s);
System.out.println(b);
*/
import java.util.Arrays;
System.out.println(Arrays.equals(a1, a2));
}
}
System.out.println(theByteArray.length);
}
}
//14
/*
* 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.
*/
/**
* @version $Id: ByteRange.java 587751 2007-10-24 02:41:36Z vgritsenko $
*/
final public class ByteRange {
2. 5. Short
2. 5. 1. Java short: short is 16 bit signed type ranges from �32,768 to 32,767.