JAVA |
PYTHON |
Java is a statically
typed language. Python is much better suited as a “glue” language.
|
Python is a
dynamically typed language. Java is better characterized as a low low-level
implementation language.
|
Requires to define
the type of each variable, it’s object oriented in the sense that you can’t
write any code without defining a class. You also invoke a compiler the code –
then you can run it.
|
No requires to
declare any variables. You can mix object-oriented and imperative
programming. You run the code directly.
|
Run faster than Python.
|
Slower than Java.
|
JAVA VS PYTHON BY COMPARING “HELLO WORLD” |
|
Hello world in Java:
Public class Main
{
Public static void main(String
[] args)
{
System.out.println(“hello
world”);
}
}
|
Hello world in
Python:
Print “hello world”;
|
JAVA VS PYTHON BY COMPARING “SIMPLE CODE EXAMPLES” |
|
String operations in
Java:
Public static void
main(String [] args)
{
String test=”compare Java with
Python”;
for(String a : test.split(“
“))
System.out.println(a);
}
|
String operations in
Python:
A=”compare Python
with Java”;
Print a.split();
|
JAVA VS PYTHON BY COMPARING “CONTROL FLOW” |
|
Control flow in Java:
int condition=10;
//if
If(condition>10)
System.out.println(“>10”);
else
System.out.println(“<10”);
//while
while(condition>1){
System.out.println(condition);
Condition--;
}
//switch
switch(condition){
case 1:
System.out.println(“is
1”);
break;
case 2:
System.out.println(“is
2”);
break;
}
//for
for(int
i=0;i<10;i++){
System.out.println(1);
}
|
Control flow in
Python:
# if
if condition >
10;
print “>10”;
elif condition ==10;
print”=10”;
else:
print”<10”;
# while
while condition >
1;
print condition;
condition= condition-1;
# switch
def f(x):
return {
1 : 1,
2 : 2,
}[x]
print f(condition);
# for
for x in
range(1,10):
print x;
|
JAVA VS PYTHON BY COMPARING “CLASS AND INHERITANCE” |
|
Class &
Inheritance in Java:
class Animal{
private String name;
public Animal(String name){
this.name=name;
}
}
class Dog extends
Animal{
public Dog(String name){
super(name);
}
}
public class Main {
public static void
main(String[] args)
{
Dog dog=new
Dog("Chiwawa");
dog.saySomething();
}
}
|
Class &
Inheritance in Python:
Class Animal():
def __init__(self, name):
self.name=name
def saySomething(self):
print "I
am " + self.name
class Dog(Animal):
def saySomething(self):
print "I
am " + self.name\+ ", and I can bark"
dog=DOg("Chiwawa")
dog.saySomething()
|
JAVA VS PYTHON BY COMPARING “FILE I/O” |
|
File I/O in Java:
// get current
directory
File dir = new
File(".");
File fin = new
File(dir.getCannonicalPath() + File.seperator + "Code.txt");
FileInputStream fis
= new FileInputStream(fin);
// Construct the
BufferedReader object
BufferedReader in =
new BufferedReader(new InputStreamReader(fis));
String aLine = null;
while ((aLine =
in.readline()) != null)
{//Process each
line, here we count empty lines
if(aLine.trim().length()==0){
}
}
//Do not forget to
close the buffer reader
in.close();
|
File I/O in Python:
myFile =
open("/home/xiaoran/Desktop/test.txt")
print myFile.read();
|
JAVA VS PYTHON BY COMPARING “COLLECTIONS” |
|
Collections in Java:
import
java.util.ArrayList;
public class Main{
public static void
main(String[] args)
{
ArrayList<String>
a1=new ArrayList<String>();
a1.add("a");
a1.add("b");
a1.add("c");
System.out.println(a1);
}
}
|
Collections in
Python:
aList=[]
aList.append("a");
aList.append("b");
aList.append("c");
print aList;
|
JAVA VS PYTHON BY COMPARING “NUMBERS” |
|
Numbers in Java:
//Integer numbers
int num = 100;
//Floating point
numbers
float f = 1.01f;
//float f =
1.01;//Wrong!
double d = 1.01;
|
Numbers in Python:
# Integer numbers:
num = 100
num =
int("100")
# Floating point
numbers
f = 1.01
f =
float("1.01")
# null
spcial = None
|
JAVA VS PYTHON BY COMPARING "NULL" |
|
Null in Java:
//null
object special =
null;
|
Null in Python:
# null
spcial = None
|
JAVA VS PYTHON BY COMPARING "TUPLES" |
|
Tuples in Java:
No tuples in Java.
|
Tuples in Python:
aTuple = ()
aTuple = (5) # cause
error
aTuple = (5,)
print aTuple
print aTuple
#5
|
JAVA VS PYTHON BY COMPARING "SETS" |
|
Sets in Java:
//Hashset
HashSet<String<
aSet = new HashSet<String>();
aSet.add("aaaa");
aSet.add("bbbb");
aSet.add("cccc");
aSet.add("dddd");
//Iterate over set
Iterator<String>.iterator
= aSet.iterator();
while
(iterator.hasNext()){
System.out.println(iterator.next()
+ " ");
}
HashSet<String>
bSet = new HashSet<String>();
bSet.add("eeee");
bSet.add("ffff");
bSet.add("gggg");
bSet.add("dddd");
//Check if bSet is a
subset of aSet
boolean b =
aSet.ContainsAll(bSet);
//Union - transform
aSet
//into the union of
aSet and bSet
aSet.addAll(bSet);
//intersection -
transform aSet
//into the
intersection of aSet and bSet
aSet.retainAll(bSet);
//difference -
transforms aSet
//into the
(asymmetric) set difference
// of aSet and bSet.
aSet.removeAll(bSet);
|
Sets in Python:
aSet = set()
aSet =
set("one") #a set containing three letters
#Set(['e', 'o',
'n'])
aSet = set(['one',
'two', 'three'])
#set(['three',
'two', 'one'])
#a set containing
three words
#iterate over set
for v in aSet:
print v
bSet = set(['three',
'four', 'five'])
#union
cSet = aSet | bSet
#intersection
dSet = aSet &
bSet
#find elements in
aSet not bSet
eSet =
aSet.difference(bSet)
#add element
bSet.add("six")
#set(['four', 'six',
'five', 'three'])
|
JAVA VS PYTHON BY COMPARING "DICTIONARIES" |
|
Dictonaries in Java:
HashMap<Sting,
String> phoneBook = new HashMap<String, String>();
phoneBook.put("Mike",
"555-1111");
phoneBook.put("Lucy",
"555-2222");
phoneBook.put("Mike",
"555-3333");
//iterate over
HashMap
Map<String,
String>map= new HashMap<String, String>();
for
(Map.Entry<String, String> entry; map.entrySet()){
System.out.println("Key
= " + entry.getKey() + ", Value = " + entry.getValue());
}
//get key value
phoneBook.get("Mike");
//get all key
Set keys =
phoneBook.keySet();
//get number of
elements
phoneBook.size();
//delete all
elements
phoneBook.clear();
//delete an element
phoneBook.remove("Lucy");
|
Dictonaries in
Python:
#create an empty
dictionary
phoneBook = {}
phoneBook =
{"Mike":"555-1111","Lucy":"555-2222","Jack":"555-3333"}
#iterate over dictionary
for key in
phoneBook:
print(key, phoneBook[key])
#add an element
phoneBook["Marry"]
= "5555-6666"
#delete an element
del
phoneBook["Mike"]
#get number of
elements
count =
len(phoneBook)
#can have different
types
phoneBook["SUsan"]
= (1, 2, 3, 4)
#return all keys
print
phoneBook.keys()
#delete all the
elements
phoneBook.clear()
|
This website is basically for the geeks all around the world..!! If you know it, the world should also..!! #ShArE_kNoWlEdGe.
Thursday, 16 March 2017
JAVA VS PYTHON
Subscribe to:
Post Comments (Atom)
Popular Posts
-
Facebook: Cowl image 851 x 315 Profile photo a hundred and eighty x 180 Hyperlink photograph 1200 x 627 Shared image 1200 x 900 ...
-
Google has updated its Google Play distribution data for the seven-day period ending January 4. As Google's Android 6.0 Marshmallo...
-
2Wire 192.168.1.1 192.168.0.1 192.168.1.254 10.0.0.138 3Com 192.168.1.1 192.168.1.10.1 Actiontec 192.168.1.1 192.168.0.1 192.16...

No comments:
Post a Comment