C programming vs. Java programming

Source: Internet
Author: User

Thing C Java
Type of language function oriented Object oriented
BASIC Programming Unit function class = ADT
Portability of source code Possible with discipline Yes
Portability of compiled code No, recompile for each architecture Yes, bytecode is "write once, run anywhere"
Security Limited Built-in to language
Compilation gcc hello.c creates machine language code Javac Hello.java creates Java Virtual machine language bytecode
Linking in the Math library GCC-LM calculate.c No special flags needed
Joint compilation GCC main.c helper1.c helper2.c Javac Main.java -Any dependent files is automatically re-compiled if needed
Execution a.out loads and executes program java Hello interprets byte code
Hello, world #include <stdio.h>
int main (void) {
printf ("hello\n");
return 0;
}
public class HelloWorld {
public static void Main (string[] args) {
System.out.println ("Hello");
}
}
Integer types int usually-bit 2 ' s complement;
long usually-bit 2 ' s complement
int is 2 ' s complement;
Long is a 2 ' s complement
Floating point types float usually-bit;
double usually-bit
float is a-bit IEEE 754 binary floating point;
Double is a-bit IEEE 754
Boolean type Use int: 0 for false, nonzero for true Boolean is its own type-stores value true or false
Character type Char is usually 8 bit ASCII Char is a-bit UNICODE
For loops for (i = 0; i < N; i++) for (int i = 0; i < N; i++)
Array declarations int *a = malloc (N * sizeof (*A)); Int[] A = new int[n];
Array size Arrays don ' t know their own size A.length
Strings '-terminated 'character array Built-in immutable String data type
Accessing a library #include <stdio.h> Import Java.io.File;
Accessing a library function #include "math.h"
x = sqrt (2.2);
All function and variables names is global
x = math.sqrt (2.2);
Functions have different namespaces
Printing to standard output printf ("sum =%d", x); System.out.println ("sum =" + x);
Formatted printing printf ("avg =%3.2f", avg); System.out.printf ("avg =%3.2f", avg)
Reading from stdin scanf ("%d", &x); Java library support, but easier to use our library
int x = Stdin.readint ();
Memory address Pointer Reference
manipulating pointers *, &, + No direct manipulation permitted
Functions int max (int a, int b) public static int Max (int a, int b)
Pass-by-value Primitive data types, structs, and pointers is passed by value; array decays to pointer All primitive data types and references (which includes arrays), is passed by value
Defining a data structure struct class -key difference is language support for defining methods to manipulate data
Accessing a data structure A.numerator for elements A.numerator For instance variables,
C = a.plus (b) for methods
Pointer chasing X->left->right X.left.right
Allocating memory Malloc New
De-allocating Memory Free Automatic garbage collection
Memory allocation of data structures and arrays Heap, stack, data, or BSS Heap
Buffer overflow Segmentation fault, Core dump, unpredicatable program Checked Run-time Error exception
Declaring constants Const and #define Final
Variable auto-initialization Not guaranteed Instance variables (and array elements) initialized to 0, null, or false, compile-time error to Access uninitialized variables
Data hiding Opaque pointers and static Private
Interface method non-static function Public method
Data type for generic item void * Object
Casting Anything Goes Checked exception at run-time or Compile-time
Demotions Automatic, but might lose precision Must explicitly cast, e.g., to convert from a long to int
Polymorphism Union Inheritence
Overloading No Yes methods, no for operators
Graphics Use external libraries Java library support, with our standard drawing library
Null Null Null
Enumeration Enum Typesafe enum
Preprocessor Yes No
Variable declaration At beginning of a block Before you use it
Variable Naming conventions Sum_of_squares Sumofsquares
Commenting /* */ / * * /or//
File Naming conventions stack.c, stack.h Stack.java -file name matches name of class
Callbacks Pointers to global functions Use interfaces for Commmand dispatching
Variable number of arguments VarArgs String ...
Assertions Assert Assert
Exit and return value to OS Exit (1) System.exit (1)

C programming vs. Java programming

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.