![]() ![]() ![]() ![]() |
| Topics this page:
|
|
|
Character | Description | Example Code | Output |
---|---|---|---|
C or c | Currency | Console.Write("{0:C}",2.5);
Console.Write("{0:C}",-2.5); | $2.50
($2.50) |
D or d | Decimal | Console.Write("{0:D5}",25); | 00025 |
E or e | Scientific | Console.Write("{0:E}",250000); | 2.500000E+005 |
F or f | Fixed-point | Console.Write("{0:F2}",25);
Console.Write("{0:F0}",25); | 25.00
25 |
G or g | General | Console.Write("{0:G}",2.5); | 2.5 |
N or n | Number | Console.Write("{0:N}",2500000); | 2,500,000.00 |
X or x | Hexadecimal | Console.Write("{0:X}",250);
Console.Write("{0:X}",0xffff); | FA
FFFF |
The presentation of numbers (for the current culture) can be automatically formatted by specifying these codes within curly braces, listed in alphabetical order:
public void WriteAsBytes(int value){ bytes[] = BitConverter.GetBytes(value); foreach(byte b in bytes){ Console.Write("0x{0:X2}", b); string message = String.Format("0x{0:X2}", b); Debug.Write(message); } Debug.WriteLine(""); }
If the above code is called by WriteAsBytes(32), then this would display 0x20 0x00 0x00 0x00.
Parsing strings is the opposite process from formatting.
This table ablove lists data types from smallest to largest such that implicit conversions can flow from top to bottom, but not upstream.
To explicitly cast to a smaller datatype in Java:
myInteger = (int) myLongInteger
java automatically casts to a larger datatype, but will throw a fit
rather than going smaller.
To expliciitly cast to a smaller datatype in C#:
long longValue = Int64.MaxValue; int intValue = (int) longValue;
Note the .MaxValue property avialable only to C# programmers.
Also, with C#, unsigned can go signed, but not the other way.
Value types | Reference types |
---|---|
allocated on the stack | allocated on the heap |
Directly contain their data | Store references to their data (known as objects) |
Each has its own copy of data | Two reference variables can reference same object |
Operations on one cannot affect another | Operations on one can affect another |
"Boxing" transforms a value type to a reference type by creating a temporary reference type "box" on the heap.
To convert a String to a double:
doubleOffset = Double.valueOf( offset ).doubleValue();
To convert a String to a floating point in Ruby (since the language is object oriented):
"1".to_i + 1
|
| ![]() ![]() ![]() |
| ![]() ![]() ![]() |
This C program (from Christopher Sawtell) tests
whether the char inherent data type is signed or unsigned.
| char a; unsigned char b; a = b = 128; a >>= 1; b >>= 1; printf ( "\nChar is %ssigned by default.\n\n", a == b ? "un" : "" ); } | ![]() ![]() ![]() |
|
Data Type | Usage | Max Bytes |
---|---|---|
binary |
Fixed length | 8,000 |
bit |
value 1 or 0 | 1 |
char |
Fixed length | 8,000 |
nchar |
Fixed length two-byte "national" Unicode | 4,000 |
Oracle-SQL has no data type boolean. The Oracle character set can be displayed by using these SQL statements:
|
Java Type | SQL Type from java.sql.Types |
---|---|
java.sql.Date | DATE |
java.sql.Time | TIME |
java.sql.Timestamp | TIMESTAMP |
java.sql.Blob | BLOB |
java.sql.Clob | CLOB |
java.sql.Array | ARRAY |
java.sql.Ref | REF |
java.sql.Struct | STRUCT |
java.math.BigDecimal | NUMERIC |
java.lang.String | VARCHAR or LONGVARCHAR |
byte[] array | VARBINARY or LONGVARBINARY |
Type conversion between SQL and Java in Castor
Unlike C or C++, Java primitive (inherent) datatypes are portable because they each occupy exactly the same amount of memory on all Java-supporting machines.
|
Built-in Javascript Function | Action | |
---|---|---|
escape(charstring) | Returns the conversion of charstring into a form that displays in the browser without HTML markup. | |
unescape(charstring) | Returns the conversion of charstring back into a form that displays in the browser with HTML markup (the opposite of escape). | |
eval(codestring) | Evaluates codestring as JavaScript code, returning anything that JavaScript returns. | |
isNaN(numvalue) | Returns true if numvalue is not a number, otherwise returns false — used with parseFloat and parseInt. | |
parseFloat(numstring) | Returns numstring converted to a floating point number. If it cannot be converted, returns the reserved value NaN. | |
parseInt(numstring) | Returns numstring converted to an integer. If it cannot be converted, returns the reserved value NaN. |
|
Data Type | Java Coding Sample | Note |
---|---|---|
Boolean | boolean isBig = true; boolean isSmall = false; | Identifier prefix is "is". True or false value spec is not encased. |
Character | char c = "w"; char c1 = '\u1234'; | Single quotes are used to encase value spec |
Integer (32 bit) | int X = 256; | |
Long Integer (64 bit) | long longL = 24445345L; | Don't use lower-case l to avoid confusion. |
Single Float | float floatA = 1.32E+2F; | Don't use lower-case f to avoid confusion. |
Double Float | float floatB = 1.32E+2D; | Don't use lower-case d to avoid confusion. |
Because, by default, a numeric literal is either a double or an int, double and float literals are declared a special way. So remember to append the symbol shown in the "Default Value" column above.
To avoid confusion, use the upper-case code (L) even though the lower case (l) is valid.
|
Constant Description | BSD/GNU Name | BSD/GNU Value |
---|---|---|
The maximum value of a single-precision floating-point number. | HUGE | 3.40282347e+38F |
Positive infinity: +infinity on machines that support IEEE-754 and DBL_MAX otherwise. | HUGE_VAL | 1e500 |
The maximum value of a non-infinite single-precision floating point number. | MAXFLOAT | 3.40282347e+38F
3.40282346638528860e+38 |
The base of natural logarithms (e). | M_E M_El | 2.7182818284590452354
2.7182818284590452353602874713526625L |
The base-2 logarithm of e. | M_LOG2E M_LOG2El | 1.4426950408889634074
1.4426950408889634073599246810018921L |
The base-10 logarithm of e. | M_LOG10E M_LOG10El | 0.43429448190325182765
0.4342944819032518276511289189166051L |
The natural logarithm of 2. | M_LN2 M_LN2l | 0.69314718055994530942
0.6931471805599453094172321214581766L |
The natural logarithm of 10. | M_LN10 M_LN10l | 2.30258509299404568402
2.3025850929940456840179914546843642L |
![]() | M_PI M_PIl | 3.14159265358979323846
3.1415926535897932384626433832795029L |
![]() | M_PI_2 M_PI_2l | 1.57079632679489661923
1.5707963267948966192313216916397514L |
![]() | M_PI_4 M_PI_4l | 0.78539816339744830962
0.7853981633974483096156608458198757L |
1/![]() | M_1_PI M_1_PIl | 0.31830988618379067154
0.3183098861837906715377675267450287L |
2/![]() | M_2_PI M_2_PIl | 0.63661977236758134308
0.6366197723675813430755350534900574L |
2/![]() | M_2_SQRTPI M_2_SQRTPIl | 1.12837916709551257390
1.1283791670955125738961589031215452L |
The positive square root of 2. | M_SQRT2 M_SQRT2l | 1.41421356237309504880
1.4142135623730950488016887242096981L |
The positive square root of 1/2. | M_SQRT1_2 M_SQRT1_2l | 0.70710678118654752440
0.7071067811865475244008443621048490L |
Bessel function limit of significance pi*2**52 — the maximum absolute value of the ordinate before we assume total loss of significance, per ISO/IEC 10967-1:1994 Information Technology - Language Independent Arithmetic - Part 1: Integer and floating-point arithmetic. Defined in <values.h> | X_TLOSS | 1.41484755040568800000e+16 |
Other constants:
Constant Description | BSD/GNU Name | BSD/GNU Value |
---|---|---|
Avogadro's number | NA | 6.025 X 1023g mole-1 |
Planck's constant | h | 6.626068 X 10-34 Js (Joule seconds) |
Free space velocity of light | c | 3.00 X 108ms-1 |
Electron charge | e | 1.602 X 10-19C |
Electron rest mass | me | 9.11 X 10-13kg |
Specific electron charge | e/m | 1.760 X 1011Ckg-1 |
Atomic mass unit | amu | 1.660 X 10-27kg |
Proton rest mass | mp | 1.6724 X 10-27kg |
Stefan-Boltzmann constant | sigma | 5.67 X 10-8Jm-2K-4S-1 |
Universal gas constant | R | 8.31JK-1g mole-1 |
Universal gravitation constant | G | 6.673 x 10-11Nm2kg-2 |
Boltzmann constant | k | 1.381 x 1023JK-1 |
Java does not automatically declare φ (Greek letter Phi
pronounced "fee") ~=1.61803... — (1+the square root of 5)/2 — the basis for the Golden Rectangles and Spirals found in nature (sunflowers, nautilus shells, and the formation of galaxies).
Phi is the limit of the ratios of the numbers in the
Fibonnacci sequence, where each number is the sum of the two before it
(q = 1 + 1/q) for
Dividing each number by the number before it (eg., 55/34) yields Phi (through convergence by oscillation).
To define constant PHI in Java:
To define constant PHI in Oracle SQL:
from the formula (1/p + 1/p2 or p3 - p - 1), called the Padovan sequence series after architect Richard Padovan of Padua (100 miles from Pisa where Fibonacci lived) [Nexus Network Journal, vol 4, no. 3 (Summer 2002)]
The ratio between adjacent numbers yield a constant ratio of 1.324718. This was nicknamed the "Plastic Number" in 1928 by its discoverer Dom Hans Van Der Laan because it generates amazing, interconnected commensurate design patterns, many of which suggest 3D applications.
|
Erroneous calculation | IEEE 754 return value |
---|---|
Divide by zero | n/a - ArithmeticException is thrown |
??? | Float.NaN |
Double.NaN | |
square root of a negative number | Float.NEGATIVE_INFINITY |
Float.POSITIVE_INFINITY | |
Double.NEGATIVE_INFINITY | |
Double.POSITIVE_INFINITY |
These literals are considered non-ordinal.
Tthe comparison x == Float.NaN returns false even if x contains Float.NaN.
So test for them using static methods Float.isNaN(float) or Double.isNaN(double)
from the java.lang package.
Method | Constant |
---|---|
.isDigit | - |
.isLetter | - |
.isLetterOrDigit | - |
.isLowerCase | .LOWERCASE_LETTER |
.isUpperCase | .UPPERCASE_LETTER |
.isSpaceChar | - |
.isDefined in Unicode | - |
.isWhitespace .isSpaceChar .isSpace | - |
- | .CONNECTOR_PUNCTUATION |
- | .MATH_SYMBOL |
To determine the properties, use the java.lang.Character class in the SDK:
With JavaScript, isNaN(numvalue) returns true if numvalue from parseFloat and parseInt is Not a Number.
|
char* s,t; const char* cs, ct;
c is an char converted to type int; int n; | |
Code | Usage |
---|---|
char *strcpy(s,ct) | copy ct into s, including ``\0''; return s |
char *strncpy(s,ct,n) | copy ncharcater of ct into s, return s |
char *strncat(s,ct) | concatenate ct to end of s; return s |
char *strncat(s,ct,n) | concatenate n character of ct to end of s, terminate with ``\0''; return s |
int strcmp(cs,ct) | compare cs and ct; return 0 if cs=ct, <0 if cs0 if cs>ct |
char *strchr(cs,c) | return pointer to first occurence of c in cs or NULL if not encountered |
size_t strlen(cs) | return length of cs |
The C language reads a string until it encounters a null character (\0). So a trick C programmers use to quickly truncate a string is to inserting a null character in that string causes all remaining characters in that string to be ignored.
strcat(dest, src); is therefore risky.
To code defensively:
strcpy(dest, src); is risky.
To code defensively,
Unlike later versions of the javac compiler, awareness of assert is disabled by default in version 1.4.
So you will have to activate it as part of the compilation command:
Java does not actually create another object if both s1 and s2 have the same value.
So initialize strings with different values or use StringBuffer.
The use of "==" between two strings will always return false because that
operator evaluates the addresses rather than the value of two objects.
So use one of these string methods to test the difference between two string objects:
Best of all, make localized comparisons using the default locale:
To compare the same key several times, or to compare strings which are likely to be different even from the first characters, convert objects for bitwise comparison using CollationKey
Insteading of hard-coding strings in programs, use a resource file method that looks up the value of the string. This allows you to dynamically change the text without recompiling.
For best performance, such as within tight running loops threads,
1) declare the size of strings, 2) use StringBuffer, and
3) append to strings rather than concatenating them with the + plus operator.
Examples:
Unlike a String, StringBuffers are mutable (can be modified).
| ![]() ![]() ![]() |
Related Topics:
![]()
| Your first name: Your family name: Your location (city, country): Your Email address: |
Top of Page ![]() Thank you! |