Difference between revisions of "Pascal for Java users"

From Free Pascal wiki
Jump to navigationJump to search
 
(22 intermediate revisions by 7 users not shown)
Line 1: Line 1:
{|   border="1" style="margin: 1em 1em 1em 0; background: #f9f9f9; border: 1px  solid; border-collapse: collapse;"
+
{{Pascal for Java users}}
!   Java  
+
 
!   [[Pascal]]  
+
== Translating common program snippets ==
!
+
The following site shows how common problems are solved in various programming languages, including Javan and FreePascal/Object Pascal: [http://rosettacode.org/wiki/Category:Pascal Rosetta Code]
 +
 
 +
== Translating Java keywords/concepts ==
 +
{| class="wikitable"
 +
! Java !! [[Pascal]] !! Additional comments
 
|-
 
|-
 
|   {  
 
|   {  
Line 66: Line 70:
 
|   /*  
 
|   /*  
 
|   {
 
|   {
 +
|   [[Comments|Comment]] start
 +
|-
 +
|   /*  
 +
|   (*
 
|   [[Comments|Comment]] start  
 
|   [[Comments|Comment]] start  
 
|-
 
|-
 
|   */  
 
|   */  
 
|   }
 
|   }
 +
|   Comment end
 +
|-
 +
|   */  
 +
|   *)
 
|   Comment end  
 
|   Comment end  
 
|-
 
|-
Line 75: Line 87:
 
|   //
 
|   //
 
|   End of line comment (only one line comment)  
 
|   End of line comment (only one line comment)  
 +
|-
 +
|   public static void main(String[] args) { }
 +
|   [[Program]] ProgramName; [[Begin]] [[End]].
 +
|   Note the period after End
 +
|-
 +
|   someType arrayVar[];
 +
|   arrayVar: [[Array]] [[Of]] someType;
 +
|  
 +
|-
 +
|   someType arrayVar[#];
 +
|   arrayVar: [[Array]][MINRANGE..MAXRANGE] [[Of]] someType;
 +
|  
 
|-
 
|-
 
|   null
 
|   null
Line 84: Line 108:
 
|  
 
|  
 
|-
 
|-
|   class
+
|   break
|   [[Class]]
+
|   [[Break]]
 
|  
 
|  
 +
|-
 +
|   class TheClass { }
 +
|   TheClass = [[Class]] [[End]];
 +
|   Delphi OOP
 +
|-
 +
|   class TheClass { }
 +
|   TheClass = [[Object]] [[End]];
 +
|   Turbo Pascal OOP
 +
|-
 +
| &nbsp; class TheClass<T> { }
 +
| &nbsp; [[Generic]] TheClass = [[Class]]<T> [[End]];
 +
| &nbsp; Generics are classes only as of 2.2.2, likely to support more in the future
 +
|-
 +
| &nbsp; TheClass()
 +
| &nbsp; [[Constructor]] CtorName
 +
| &nbsp; Constructor name is by convention either Init or Create
 
|-
 
|-
 
| &nbsp; continue
 
| &nbsp; continue
| &nbsp;
+
| &nbsp; [[Continue]]
 
| &nbsp;  
 
| &nbsp;  
 
|-
 
|-
Line 98: Line 138:
 
| &nbsp; do while ! &nbsp;
 
| &nbsp; do while ! &nbsp;
 
| &nbsp; [[Repeat]] [[Until]] &nbsp;
 
| &nbsp; [[Repeat]] [[Until]] &nbsp;
 +
| &nbsp;
 +
|-
 +
| &nbsp; enum TheEnum&nbsp;
 +
| &nbsp; TheEnum = ( MINVALUE .. MAXVALUE ); &nbsp;
 +
| &nbsp;
 +
|-
 +
| &nbsp; enum TheEnum = {MINVALUE, MAXVALUE}&nbsp;
 +
| &nbsp; TheEnum = ( MINVALUE, MAXVALUE ); &nbsp;
 +
| &nbsp;
 +
|-
 +
| &nbsp; TheEnum enumVar; &nbsp;
 +
| &nbsp; enumVar := [[Set]] [[Of]] TheEnum; &nbsp;
 
| &nbsp;
 
| &nbsp;
 
|-
 
|-
 
| &nbsp; extends
 
| &nbsp; extends
| &nbsp;
+
| &nbsp; SubClass = [[Class]](BaseClass)
 
| &nbsp; class SubClass extends BaseClass    --> SubClass(BaseClass)
 
| &nbsp; class SubClass extends BaseClass    --> SubClass(BaseClass)
 
|-
 
|-
 
| &nbsp; final
 
| &nbsp; final
| &nbsp;
+
| &nbsp; [[Const]]
| &nbsp;
+
| &nbsp; for constants, not uninheritables
 
|-  
 
|-  
 
| &nbsp; for ++ &nbsp;
 
| &nbsp; for ++ &nbsp;
Line 125: Line 177:
 
|-
 
|-
 
| &nbsp; implements
 
| &nbsp; implements
| &nbsp;
+
| &nbsp; SomeClass = [[Class]](SomeInterface)
| &nbsp;
+
| &nbsp;  
 
|-
 
|-
 
| &nbsp; import
 
| &nbsp; import
Line 137: Line 189:
 
|-
 
|-
 
| &nbsp; interface
 
| &nbsp; interface
 +
| &nbsp; TheInterface = [[Interface]]
 
| &nbsp;
 
| &nbsp;
| &nbsp;
 
 
|-
 
|-
 
| &nbsp; native
 
| &nbsp; native
| &nbsp;
+
| &nbsp; [[StdCall]]
| &nbsp;
+
| &nbsp; For Windows
 +
|-
 +
| &nbsp; native
 +
| &nbsp; [[CDecl]]
 +
| &nbsp; For Unix
 +
|-
 +
| &nbsp; new primitive_type[#];
 +
| &nbsp; [[SetLength]](ArrayVar, #);
 +
| &nbsp;  &nbsp;
 
|-
 
|-
| &nbsp; new
+
| &nbsp; new Class();
| &nbsp;  
+
| &nbsp; InstanceVar := TheClass.Create;
| &nbsp; ClassType Some = new ClassType(); --> Some := ClassType.Create; &nbsp;
+
| &nbsp;  &nbsp;
 
|-
 
|-
| &nbsp; package
+
| &nbsp; package pkgName;
| &nbsp; [[Unit]]
+
| &nbsp; [[Unit]] unitName; [[Interface]] [[Implementation]] [[End]].
| &nbsp;
+
| &nbsp; Note the period after End
 
|-
 
|-
 
| &nbsp; private
 
| &nbsp; private
Line 165: Line 225:
 
|-
 
|-
 
| &nbsp; return
 
| &nbsp; return
| &nbsp; [[Result]]
+
| &nbsp; FunctionName :=
 +
| &nbsp;
 +
|-
 +
| &nbsp; return
 +
| &nbsp; [[Result]] :=
 +
| &nbsp; ObjFPC or Delphi modes
 +
|-
 +
| &nbsp; return
 +
| &nbsp; [[Function#return value|Exit()]]
 +
| &nbsp; ObjFPC modes
 +
|-
 +
| &nbsp; static
 +
| &nbsp; [[Static]]
 
| &nbsp;
 
| &nbsp;
 
|-
 
|-
 
| &nbsp; static
 
| &nbsp; static
 +
| &nbsp; [[Class]] [[Function]]
 
| &nbsp;
 
| &nbsp;
 +
|-
 +
| &nbsp; static
 +
| &nbsp; [[Class]] [[Procedure]]
 
| &nbsp;
 
| &nbsp;
 
|-
 
|-
 
| &nbsp; super
 
| &nbsp; super
 
| &nbsp; [[Inherited]]
 
| &nbsp; [[Inherited]]
| &nbsp;
+
| &nbsp; Parent constructor call
 
|-
 
|-
 
| &nbsp; switch case break &nbsp;
 
| &nbsp; switch case break &nbsp;
Line 185: Line 261:
 
|-
 
|-
 
| &nbsp; synchronized
 
| &nbsp; synchronized
| &nbsp;
+
| &nbsp; [[TCriticalSection]]
 
| &nbsp;
 
| &nbsp;
 
|-
 
|-
Line 205: Line 281:
 
|-
 
|-
 
| &nbsp; try { } catch
 
| &nbsp; try { } catch
| &nbsp; [[Try]] [[except]]
+
| &nbsp; [[Try]] [[Except]]
 
| &nbsp;
 
| &nbsp;
 
|-
 
|-
Line 225: Line 301:
 
|}
 
|}
  
 
+
== Translating Java data types ==
{| &nbsp; border="1" style="margin: 1em 1em 1em 0; background: #f9f9f9; border: 1px  solid; border-collapse: collapse;"
+
{| class="wikitable"
! &nbsp; Java type &nbsp;
+
! Java type !! [[Pascal]] [[Data_type|type]] !! Size (bits) !! Range !!
! &nbsp; [[Pascal]] [[Data_type|type]] &nbsp;
 
! &nbsp; Size (bits) &nbsp;
 
! &nbsp; Range &nbsp;
 
! &nbsp;
 
 
|-
 
|-
 
| &nbsp; byte &nbsp;
 
| &nbsp; byte &nbsp;
Line 240: Line 312:
 
|-
 
|-
 
| &nbsp; short &nbsp;
 
| &nbsp; short &nbsp;
| &nbsp; [[SmallInt]]
+
| &nbsp; [[Smallint]]
 
| &nbsp; 16-bit
 
| &nbsp; 16-bit
 
| &nbsp; -32768 .. 32767 &nbsp;
 
| &nbsp; -32768 .. 32767 &nbsp;
|
 
 
|-
 
|-
 
| &nbsp; int &nbsp;
 
| &nbsp; int &nbsp;
| &nbsp; [[LongInt]]
+
| &nbsp; [[Longint]]
 +
| &nbsp; 32-bit
 +
| &nbsp; -2147483648..2147483647 &nbsp;
 +
| &nbsp; 
 +
|-
 +
| &nbsp; int &nbsp;
 +
| &nbsp; [[Integer]]
 
| &nbsp; 32-bit
 
| &nbsp; 32-bit
 
| &nbsp; -2147483648..2147483647 &nbsp;
 
| &nbsp; -2147483648..2147483647 &nbsp;
Line 252: Line 329:
 
|-
 
|-
 
| &nbsp; long &nbsp;
 
| &nbsp; long &nbsp;
| &nbsp; [[Comp]]
+
| &nbsp; [[Int64]]
 
| &nbsp; 64-bit
 
| &nbsp; 64-bit
 
| &nbsp; -9 223 372 036 854 775 808 .. 9 223 372 036 854 775 807  &nbsp;
 
| &nbsp; -9 223 372 036 854 775 808 .. 9 223 372 036 854 775 807  &nbsp;
Line 278: Line 355:
 
| &nbsp; [[WideChar]]
 
| &nbsp; [[WideChar]]
 
| &nbsp; 16-bit
 
| &nbsp; 16-bit
 +
| &nbsp;
 +
| &nbsp;
 +
|-
 +
| &nbsp; String &nbsp;
 +
| &nbsp; [[String]]
 
| &nbsp;  
 
| &nbsp;  
 
| &nbsp;
 
| &nbsp;
Line 283: Line 365:
 
|-
 
|-
 
|}
 
|}
 +
 +
== See also ==
 +
* [[Using Pascal Libraries with Java]]

Latest revision as of 21:00, 4 March 2019

Deutsch (de) English (en)

Translating common program snippets

The following site shows how common problems are solved in various programming languages, including Javan and FreePascal/Object Pascal: Rosetta Code

Translating Java keywords/concepts

Java Pascal Additional comments
  {     Begin    
  }     End  
  =     :=   Becomes  
  ==     =   Equal
  /     /   Division (or sometimes div)  
  %     Mod   Modulo operation
  !     Not  
  !=     <>   Not equal  
  &&     And  
  ||     Or  
  ^     Xor  
  >>     Shr   bit shift right  
  <<     Shl   bit shift left  
  ++     Inc  
  --     Dec  
  /*     {   Comment start
  /*     (*   Comment start
  */     }   Comment end
  */     *)   Comment end
  //     //   End of line comment (only one line comment)  
  public static void main(String[] args) { }   Program ProgramName; Begin End.   Note the period after End
  someType arrayVar[];   arrayVar: Array Of someType;  
  someType arrayVar[#];   arrayVar: Array[MINRANGE..MAXRANGE] Of someType;  
  null   Nil  
  abstract   Abstract  
  break   Break  
  class TheClass { }   TheClass = Class End;   Delphi OOP
  class TheClass { }   TheClass = Object End;   Turbo Pascal OOP
  class TheClass<T> { }   Generic TheClass = Class<T> End;   Generics are classes only as of 2.2.2, likely to support more in the future
  TheClass()   Constructor CtorName   Constructor name is by convention either Init or Create
  continue   Continue  
  do while     Repeat Until Not   
  do while !     Repeat Until    
  enum TheEnum    TheEnum = ( MINVALUE .. MAXVALUE );    
  enum TheEnum = {MINVALUE, MAXVALUE}    TheEnum = ( MINVALUE, MAXVALUE );    
  TheEnum enumVar;     enumVar := Set Of TheEnum;    
  extends   SubClass = Class(BaseClass)   class SubClass extends BaseClass --> SubClass(BaseClass)
  final   Const   for constants, not uninheritables
  for ++     For To Do  
  for --     For Downto Do    
  if()     If Then  
  if() else     If Then Else  
  implements   SomeClass = Class(SomeInterface)  
  import   Uses  
  instanceof   Is  
  interface   TheInterface = Interface  
  native   StdCall   For Windows
  native   CDecl   For Unix
  new primitive_type[#];   SetLength(ArrayVar, #);    
  new Class();   InstanceVar := TheClass.Create;    
  package pkgName;   Unit unitName; Interface Implementation End.   Note the period after End
  private   Private  
  protected   Protected  
  public   Public  
  return   FunctionName :=  
  return   Result :=   ObjFPC or Delphi modes
  return   Exit()   ObjFPC modes
  static   Static  
  static   Class Function  
  static   Class Procedure  
  super   Inherited   Parent constructor call
  switch case break     Case Of End  
  switch case break default     Case Of Else End    
  synchronized   TCriticalSection  
  this   Self  
  throw   Raise  
  throws    
  transient    
  try { } catch   Try Except  
  try { } catch finally   Try Finally  
  void   Procedure  
  volatile    
  while     While Do  

Translating Java data types

Java type Pascal type Size (bits) Range
  byte     Shortint   8-bit   -128 .. 127  
  short     Smallint   16-bit   -32768 .. 32767  
  int     Longint   32-bit   -2147483648..2147483647    
  int     Integer   32-bit   -2147483648..2147483647    
  long     Int64   64-bit   -9 223 372 036 854 775 808 .. 9 223 372 036 854 775 807    
  float     Single   32-bit   1.5E-45 .. 3.4E+38  
  double     Double   64-bit   5.0E-324 .. 1.7E+308  
  boolean     Boolean     False True  
  char     WideChar   16-bit    
  String     String    

See also